【贪心】HDU 5783 Divide the Sequence

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5783

题目大意:

  把一个N个数的数列拆成若干段,保证每一段的前缀和都非负,求最多能拆成多少段。

题目思路:

  【贪心】

  一开始题目看错了看成每一段内和非负。。DPWA了好久。

  默认答案是n,从后往前找负数,找到一个负数就一直把它往前合并直到和值非负,这样这个区间的前缀和就一定非负,扣除合并的区间大小即可。

 

 1 //
 2 //by coolxxx
 3 //#include<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define mod 1000000007
26 #define MAX 0x7f7f7f7f
27 #define PI 3.14159265358979323
28 #define N 1000004
29 using namespace std;
30 typedef long long LL;
31 int cas,cass;
32 int n,m,lll,ans;
33 LL a[N];
34 int main()
35 {
36     #ifndef ONLINE_JUDGE
37 //    freopen("1.txt","r",stdin);
38 //    freopen("2.txt","w",stdout);
39     #endif
40     int i,j,k;
41 //    for(scanf("%d",&cas);cas;cas--)
42 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
43 //    while(~scanf("%s",s+1))
44     while(~scanf("%d",&n))
45     {
46         for(i=1;i<=n;i++)
47             scanf("%lld",&a[i]);
48         ans=n;
49         for(i=n;i;i--)
50         {
51             if(a[i]<0)
52             {
53                 j=i;
54                 while(a[j]<0)a[j]+=a[--i];
55                 ans-=j-i;
56             }
57         }
58         printf("%d\n",ans);
59     }
60     return 0;
61 }
62 /*
63 //
64 
65 //
66 */
View Code

 

posted @ 2016-08-27 18:23  Cool639zhu  阅读(154)  评论(0编辑  收藏  举报