DP is a common problem in the competition, but also my weakness orz, more to practice. See Ganga's DP topic exercise the first is Max sum plus Plus, so I took the hdu1003 max sum I've done before and did it again.
HDU 1003 Max Sum
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1003
Title Description: A sequence of integers, in which a continuous sub-sequence is obtained, and the maximum value and the starting subscript of the sequence are given.
Idea: Simple DP, seize the "continuous" to start. Construct pair<int,int> Dp[i] is the maximum of successive sub-sequences ending with a[i] and the beginning of this sequence, then it is very simple to derive Dp[i].first=max (Dp[i-1].first+a[i],a[i]) because a[ I] at the end there are only two cases: 1, sticking to a[i-1] end of the sub-sequence, then the largest and is the front of the largest and plus a[i], the starting point is the front of the starting point, 2, A[i] from a new sequence, then the largest and is its own, starting point is its own, compared to that kind of situation is
In addition, because Dp[i] is only related to dp[i-1], so actually only need to re-use a variable on the line, which is written in the array to see a little
Code:
#include <iostream>#include<ios>#include<iomanip>#include<functional>#include<algorithm>#include<vector>#include<sstream>#include<list>#include<queue>#include<deque>#include<stack>#include<string>#include<Set>#include<map>#include<cstdio>#include<cstdlib>#include<cctype>#include<cmath>#include<cstring>#include<climits>using namespacestd;#defineXinf Int_max#defineINF 1<<30#defineMAXN 100000+10#defineEPS 1e-10#defineZero (a) fabs (a) <eps#defineSqr (a) ((a) * (a))#defineMP (x, y) make_pair (x, y)#definePB (x) push_back (x)#definePF (x) push_front (x)#defineREP (x,n) for (int x=0; x<n; X + +)#defineREP2 (X,L,R) for (int x=l; x<=r; X + +)#defineDEP (x,r,l) for (int x=r; x>=l; x--)#defineCLR (a,x) memset (a,x,sizeof (A))#defineIT iterator#definePI ACOs (-1.0)#defineTest puts ("OK");#define_ Ios_base::sync_with_stdio (0); Cin.tie (0);typedefLong LongLl;typedef pair<int,int>pii;typedef priority_queue<int,vector<int>,greater<int> >pqi;typedef Vector<PII>vii;typedef Vector<int>VI;#defineX First#defineY SecondPII DP[MAXN];intNUM[MAXN];intMain () {_intT; scanf ("%d",&T); REP (k,t) {intN; CLR (DP,0); scanf ("%d",&N); REP (i,n) scanf ("%d",&Num[i]); dp[0]=MP (num[0],0); for(intI=1; i<n;i++) { if(dp[i-1]. x+num[i]>=Num[i]) {Dp[i]. X=dp[i-1]. x+Num[i]; Dp[i]. Y=dp[i-1]. Y } Else{Dp[i]. X=Num[i]; Dp[i]. Y=i; } } intmax=-inf,ans1=0, ans2=0; REP (i,n) {if(Dp[i]. X>Max) {Max=Dp[i]. X Ans1=dp[i]. y+1; Ans2=i+1; }} printf ("Case %d:\n", K +1); printf ("%d%d%d\n", MAX,ANS1,ANS2); if(k<t-1) printf ("\ n"); } return 0;}
View Code
HDU Max Sum plus Plus
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1024
Title Description: As with 1003, here additionally M, which is the maximum and of the continuous sub-sequences of the M-segment disjoint. For example, when the sequence is 1 4-2 3-2 3,m=2, the maximum value is 8 (4 and 3,-2,3 two segments)
Train of thought: constructs Dp[i][j] for the a[i] end of the J-series of the largest and, of course, j=1 is actually the above problem. The state transfer equation is similar: 1, sticking to the end of the J-series A[i-1, then the dp[i][j]=dp[i-1][j]+a[i],2, since a new sequence, at this time to note that because several paragraphs can be separated between the sequence, so it is not simple dp[i-1][j-1] +a[i], but in Max (Dp[i-1][j-1],dp[i-2][j-1],..., dp[j-1][j-1]) +a[i]. Because the range of n is large, the two-dimensional array cannot be opened, but fortunately, the first layer of DP is only related to the i-1 layer, and one-dimensional arrays can be reused. In another case 2 need to find Max again and again, will time out, you can see DP[I][J] must be larger than the previous layer, so you can use the array Posmax to record the location of the largest number of times, the next layer can be directly used to save time.
Time complexity is O (nm), it is strange that the problem does not give the range of M, so that all AC appears to be relatively small data (-_-b Khan)
#include <iostream>#include<ios>#include<iomanip>#include<functional>#include<algorithm>#include<vector>#include<sstream>#include<list>#include<queue>#include<deque>#include<stack>#include<string>#include<Set>#include<map>#include<cstdio>#include<cstdlib>#include<cctype>#include<cmath>#include<cstring>#include<climits>using namespacestd;#defineXinf Int_max#defineINF 0x7fffffff#defineMAXN 1000000+10#defineEPS 1e-8#defineZero (a) fabs (a) <eps#defineSqr (a) ((a) * (a))#defineMP (x, y) make_pair (x, y)#definePB (x) push_back (x)#definePF (x) push_front (x)#defineREP (x,n) for (int x=0; x<n; X + +)#defineREP2 (X,L,R) for (int x=l; x<=r; X + +)#defineDEP (x,r,l) for (int x=r; x>=l; x--)#defineCLR (a,x) memset (a,x,sizeof (A))#defineIT iterator#definePI ACOs (-1.0)#defineTest puts ("OK");#define_ Ios_base::sync_with_stdio (0); Cin.tie (0);typedefLong LongLl;typedef pair<int,int>pii;typedef priority_queue<int,vector<int>,greater<int> >pqi;typedef Vector<PII>vii;typedef Vector<int>VI;#defineX First#defineY SecondintA[MAXN];intDP[MAXN];intPosmax[maxn];inlineint_max (intXinty) { returnX>y?x:y;}intMain () {_//freopen ("OUT.txt", "w", stdout); intM,n; while(~SCANF ("%d%d",&m,&N)) { for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); Posmax[i]=-INF; Dp[i]=-INF; } intans=-INF; for(intI=1; i<=n;i++) { intt=min (i,m); for(intj=1; j<=t;j++)//J cannot be larger than I, and j>m is meaningless, so just cycle to min (i,m){Dp[j]=_max (dp[j],posmax[j-1])+A[i]; if(j==m) ans=_max (Ans,dp[j]); } for(intj=1; j<=t;j++) Posmax[j]=_max (Posmax[j],dp[j]); /*printf ("DP:"); for (int j=1;j<=t;j++) printf ("\t%d", Dp[j]); printf ("\NPM:"); for (int j=1;j<=t;j++) printf ("\t%d", Posmax[j]); printf ("\ n"); */} printf ("%d\n", ans); } return 0;}/*3 10-3 5 6-4 0 1 3-2 10 1 output: 264 15 1-6 0 4-9-8 7 8 0-1 3-8 2 5 1 output:*/
View Code
hdu1003 Max Sum&max Sum plus plus "base DP"