hdu 5783 Divide the Sequence 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5783
题意:给你n个整数,将它分成若干个前缀和大于等于0的块,求块的最大个数。
题解:题目保证有解。从后向前贪心,当前和大于等于0时,序列个数增加一。
1 #include<bits/stdc++.h> 2 typedef long long LL; 3 const int maxn = 1000000 + 50; 4 int a[maxn]; 5 6 int main(){ 7 int n; 8 while(scanf("%d",&n) == 1){ 9 for(int i = 1; i <= n; ++i){ 10 scanf("%d",&a[i]); 11 } 12 LL now = 0; 13 int ans = 0; 14 for(int i = n; i > 0; --i){ 15 now += a[i]; 16 if(now >= 0){ 17 now = 0; 18 ans++; 19 } 20 } 21 printf("%d\n",ans); 22 } 23 return 0; 24 }