HDU4193 Non-negative Partial Sums(单调队列)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 const int INF =1<<27;
 7 struct node
 8 {
 9     int i,num;
10     node(){}
11     node(int a,int b){i=a;num=b;}
12     bool operator < (const node &aa) const
13     {
14         if(aa.num!=num) return num<aa.num;
15         return i>aa.i;
16     }
17 };
18 int an[1000010];
19 node arr[2000010];
20 int front,rear;
21 void push(node a)
22 {
23     while(front<=rear&&a<arr[rear]) rear--;
24     arr[++rear]=a;
25 }
26 int main()
27 {
28     int n,i,sum0,sum1;
29     while(scanf("%d",&n),n)
30     {
31         sum0=front=rear=0;
32         push(node(-1,-INF));
33         for(i=0;i<n;i++)
34         {
35             scanf("%d",an+i);
36             sum1=sum0+an[i];
37             push(node(i,sum1));
38             sum0=sum1;
39         }
40         if(sum1<0)
41         {
42             printf("0\n");
43             continue;
44         }
45         int tmp=0,ans=0;
46         for(i=0;i<n;i++)
47         {
48             while(arr[front].i<i) front++;
49             if(arr[front].num-tmp>=0) ans++;
50             tmp+=an[i];
51             sum1=sum0+an[i];
52             push(node(n+i,sum1));
53             sum0=sum1;
54         }
55         printf("%d\n",ans);
56     }
57     return 0;
58 }

 

posted @ 2012-10-08 10:06  qijinbiao1  阅读(330)  评论(0编辑  收藏  举报