码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5783 - Divide the Sequence

时间:2016-08-08 15:43:17      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

题意:

  给出一组数列a,问能够尽可能多的分成几段,让每一段的每一位的前缀和均 >= 0
    
分析:

  从后往前扫,将第 i 位看作为某段最后一位

    1. 若末位数字 >= 0 ,则自成一段

    2. 若末位数字 <  0 ,则向前扫直到末位前缀和 >= 0

      因为 末位的前缀和 >= 0, 则每一位的前缀和均 >= 0

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int MAXN = 1e6+5;
 5 int n;
 6 int a[MAXN];
 7 int main()
 8 {
 9     while(~scanf("%d", &n))
10     {
11         for (int i = 1; i <= n; i++)
12             scanf("%d", &a[i]);
13         long long x = 0;
14         int ans = 0;
15         for (int i = n; i >= 1; i--)
16         {
17             if(x < 0)
18             {
19                 x+=a[i];
20                 if(x>=0) x = 0, ans++;
21             }
22             else if(a[i] < 0)  x = a[i];
23             else ans++;
24         }
25         printf("%d\n", ans);
26     }
27 } 

 

HDU 5783 - Divide the Sequence

标签:

原文地址:http://www.cnblogs.com/nicetomeetu/p/5749515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!