zoukankan      html  css  js  c++  java
  • HDU 5783 Divide the Sequence

    题目链接:

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

    Problem Description
    Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfying that for each subsequence, every its prefix sum is not small than 0.
     

    Input

    The input consists of multiple test cases. 

    Each test case begin with an integer n in a single line.
    The next line contains n integers A1,A2An.
    1n1e6
    10000A[i]10000
    You can assume that there is at least one solution.
     

    Output

    For each test case, output an integer indicates the maximum number of sequence division.
     

    Sample Input

    6
    1 2 3 4 5 6
    4
    1 2 -3 0
    5
    0 0 0 0 0
     

    Sample Output

    6
    2
    5
     
    Hint
    题意:

    给你n个数,让你分成最多的块,使得每一块的任何前缀,都是大于0的。

    问你最多分成多少块。

    题解:

    用贪心的思想,从后往前就行。

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn=1e6+10;
    #define ll long long int
    int a[maxn];
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=0;i<n;i++)
                scanf("%d",&a[i]);
            ll sum=0,cnt=0;
            for(ll i=n-1;i>=0;i--)
            {
                sum+=a[i];
                if(sum>=0)
                {
                    sum=0;
                    cnt++;
                }
            }
            printf("%d
    ",cnt);
        }
    }
    

      

     
  • 相关阅读:
    关于AJAX与form表单提交数据的格式
    MongoDB
    Redis
    在django中使用django_debug_toolbar进行日志记录
    python第三方库,你要的这里都有
    Django之用户认证auth模块
    Django中常用命令
    form表单钩子,局部钩子和全局钩子
    当我开始爱自己
    FOR YOU
  • 原文地址:https://www.cnblogs.com/TAT1122/p/5730657.html
Copyright © 2011-2022 走看看