Topic Links:
http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1560
Main Topic :
Given n (n<=200) tasks and the time-consuming of each task, ask M (m<=200) time for the maximum benefit (the sum of the squares of the number of consecutive tasks to be solved , see the topic for specific examples)
Topic Ideas:
"Dynamic Planning"
Set F[i][j] represents the first I task, the current time is the optimal value of J.
The enumeration of the I-task is a task that has several and I continuous tasks, and the state transfer equation is well-pushed.
Time complexity is much smaller than O (N3), probably O (N2) level.
1 //2 //by Coolxxx3 //4#include <iostream>5#include <algorithm>6#include <string>7#include <iomanip>8#include <memory.h>9#include <time.h>Ten#include <stdio.h> One#include <stdlib.h> A#include <string.h> -#include <stdbool.h> -#include <math.h> the #defineMin (a) < (b) ( A):(B)) - #defineMax (a) (a) > (b)? ( A):(B)) - #defineABS (a) ((a) >0? ( A):(-(a))) - #defineLowbit (a) (a& (a)) + #defineSqr (a) ((a) * (a)) - #defineSwap (a) (a) ^= (b), (b) ^= (a), (a) ^= (b)) + #defineEPS 1e-8 A #defineJ 10000 at #defineMAX 0x7f7f7f7f - #definePI 3.1415926535897 - #defineN 204 - using namespacestd; - intN,m,lll,ans,cas; - intA[n],sum[n]; in intF[n][n]; - intMain () to { + #ifndef Online_judge - //freopen ("1.txt", "R", stdin); the //freopen ("2.txt", "w", stdout); * #endif $ inti,j,k;Panax Notoginsengscanf"%d",&CAs); - while(cas--) the //while (~scanf ("%s", S1)) + //while (~scanf ("%d", &n)) A { thescanf"%d%d",&n,&m); +memset (SUM,0,sizeof(sum)); -Memset (F,0,sizeof(f)); $ for(i=1; i<=n;i++) $ { -scanf"%d",&a[i]); -sum[i]=sum[i-1]+A[i]; the } - for(i=1; i<=n;i++) f[i][a[i]]=1;Wuyi for(i=1; i<=n;i++) the { - for(j=0; j<=m;j++) F[i][j]=max (f[i][j],f[i-1][j]); Wu for(j=a[i];j<=m;j++) - { AboutF[i][j]=max (f[i][j],f[i][j-1]); $ for(K=i;k && j>=sum[i]-sum[k-1];k--) - { -F[i][j]=max (f[i][j],f[k-1][j-sum[i]+sum[k-1]]+SQR (i-k+1)); - } A } + } theprintf"%d\n", F[n][m]); - } $ return 0; the } the the the /* - // in the // the */View Code
"Dynamic Planning" XMU 1560 new ACM Rules