hdu_5783_Divide the Sequence(贪心)

题目链接:hdu_5783_Divide the Sequence

题意:

给你一个数列,让你分尽可能多的段,并且保证每一段的前缀和都不小于0

题解:

从后往前xjb贪心就行了

 1 #include<cstdio>
 2 
 3 const int N=1e6+7;
 4 int a[N];
 5 int main()
 6 {
 7     int n;
 8     while(~scanf("%d",&n))
 9     {
10         for(int i=1;i<=n;i++)scanf("%d",a+i);
11         long long sum=0,ans=0;
12         for(int i=n;i>=1;i--)
13         {
14             sum+=a[i];
15             if(sum>=0)ans++,sum=0;
16         }
17         printf("%lld
",ans);
18     }
19     return 0;
20 }
View Code
原文地址:https://www.cnblogs.com/bin-gege/p/5730544.html