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

      

     
  • 相关阅读:
    网络流24题题解
    NOIP2018游记
    AGC016题解
    雅礼集训总结
    数学相关【真·NOIP】
    NOIP2018系列
    洛咕P4180 严格次小生成树
    矩阵乘法学习笔记
    一些比较神奇的思路
    点分治复习记
  • 原文地址:https://www.cnblogs.com/TAT1122/p/5730657.html
Copyright © 2011-2022 走看看