Divide the Sequence
Time limit:5000/2500 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 119 Accepted Submission (s): 73
Problem Descriptionalice have a sequence A, She wants to split a into as much as possible continuous subsequences, SATISFYI Ng, for each subsequence, the every its prefix sum are not small than 0.
Inputthe input consists of multiple test cases.
Each test case is begin with a integer n
The next line containsNIntegersa1,a2? An .
1≤n≤1e6
−10000≤A[i]≤10000
You can assume this there is at least one solution.
Outputfor each test case, output an integer indicates the maximum number of sequence division.
Sample Input61 2 3 4 5 641 2-3 050 0 0 0 0
Sample Output625
Authorzstu
Greedy. Choose as much as possible. Considering that the prefix cannot be negative, you can sweep it backwards.
/************************************************author:guanjuncreated TIME:2016/8/2 12:04:29File Name:p 512.cpp*************************************************/#include<iostream>#include<cstring>#include<cstdlib>#include<stdio.h>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<math.h>#include<stdlib.h>#include<iomanip>#include<list>#include<deque>#include<stack>#defineull unsigned long Long#definell Long Long#defineMoD 90001#defineINF 0x3f3f3f3f#defineMAXN 1000010#defineCLE (a) memset (A,0,sizeof (a))Constull inf = 1LL << A;Const Doubleeps=1e-5;using namespaceStd;priority_queue<int,vector<int>,greater<int> >PQ;structnode{intx, y;};structcmp{BOOL operator() (Node A,node b) {if(a.x==b.x)returnA.y>b.y; returnA.x>b.x; }};BOOLcmpintAintb) { returnA>b;}intA[MAXN];intVIS[MAXN];intMain () {#ifndef Online_judge//freopen ("In.txt", "R", stdin); #endif //freopen ("OUT.txt", "w", stdout); intN; while(cin>>N) { intmark=0; CLE (VIS); for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); if(a[i]<0) mark=1, vis[i]=1; } if(!Mark) {printf ("%d\n", n);Continue; } ll ans=0; for(inti=n;i>=1; i--){ if(Vis[i]) {ll sum=A[i]; while(sum<0) {i--; Sum+=A[i]; }} ans++; } printf ("%i64d\n", ans); } return 0;}
HDU 5783Divide the Sequence