Test instructions: First enter an n, representing the number of subsequent n to continue the input, to find the maximum number of sub-sequences and
Idea: Dynamic programming, Maximum subsequence and (maximum sub-segment and)
Maximal sub-sequence and transfer equation: F[i]=max (f[i-1]+date[i],date[i]) can be used directly ...
The code is as follows:
1#include <iostream>2#include <cstdio>3#include <algorithm>4 5 using namespacestd;6 7 intn,date[10000],f[10000];8 9 intCMP (Const void*a,Const void*b)Ten { One return*(int*) a-* (int*) b; A } - - BOOLdatecin () the { - if(SCANF ("%d", &n)!=eof&&N) - { - for(intI=0; i<n;i++) + { -scanf"%d",&date[i]); +f[i]=0; A } at return true; - } - return false; - } - - voidSHOWF () in { - for(intI=0; i<n;i++) tocout<<f[i]<<' '; +cout<<Endl; - } the * voiddatecal () $ {Panax Notoginsengf[0]=date[0]; - for(intI=1; i<=n;i++) the { + if(f[i-1]>0) Af[i]=f[i-1]+Date[i]; the Else + { -f[i]=Date[i]; $ } $ } - //SHOWF (); -Qsort (F,n,sizeof(f[0]), CMP);//I used qsort right here. the //cout<<f[0]<< ': ' <<f[n-1]<<endl; - }Wuyi the voidshowres () - { Wu if(f[n-1]>0) -printf"The maximum winning streak is%d.\n", f[n-1]); About Else $printf"losing streak.\n"); - } - intMain () - { A while(Datecin ()) + { the datecal (); - showres (); $ } the return 0; the}
The above code uses the Qsort function, is a sort very convenient function, first simple use, after careful study
Maximum subsequence and there are a variety of solutions, here I use the dynamic planning, other methods, also write a = = Join the learning queue! Orz
UVA, 10684 the jackpot