{HDU}{4193}{Non-negative Partial Sums}
对数组进行预处理,由于是连续的可以在O(1)的时间进行判定查询,WA了N次。
#include <iostream> #include <string> #include <cstring> #include <cstdio> #include <algorithm> #include <memory> #include <cmath> #include <bitset> #include <queue> #include <vector> #include <stack> using namespace std; const int MAXN = 550; #define CLR(x,y) memset(x,y,sizeof(x)) #define MIN(m,v) (m)<(v)?(m):(v) #define MAX(m,v) (m)>(v)?(m):(v) #define ABS(x) ((x)>0?(x):-(x)) #define rep(i,x,y) for(i=x;i<y;++i) int minval[1200000]; int minvalf[1200000]; int s[1200000]; int b; int work() { int n; int i,j,tmp,k,h,val,p; while( scanf("%d",&n) && n ) { b = 0; rep(i,0,n){ scanf("%d",&s[i]); s[i] += b; b = s[i]; } val = (1<<30); for( i = n-1; i >= 0; --i) { if(s[i] < val){ val = s[i]; } minval[i] = val; } val = (1<<30); rep(i,0,n) { if( s[i] < val ) { val = s[i]; } minvalf[i] = val; } int ans = 0; minval[n] = (1<<30); rep(i,0,n){ val = minval[i+1]; if( val - s[i] >= 0 && s[n-1]-s[i]+minvalf[i] >= 0 ) ++ans; } printf("%d\n",ans); } return 0; } int main() { work(); return 0; }