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);
        }
    }
    

      

     
  • 相关阅读:
    vue模拟接口数据
    修改placeholder的颜色
    network is unreachable mongodb
    数字转时间
    前端下载流文件
    jquery的AJAX中各个事件执行顺序
    移动端谨慎使用overflow:hidden
    时间函数整理
    background-size使用时的注意点
    关于iframe
  • 原文地址:https://www.cnblogs.com/TAT1122/p/5730657.html
Copyright © 2011-2022 走看看