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

    Divide the Sequence

    Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 649    Accepted Submission(s): 331


    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
     
    Author
    ZSTU
     
    Source
     
     
     
    解析:要使所有前缀和不小于0,只需从后向前扫描整个序列a[],如果a[i]非负,则a[i]是一个满足条件的子序列;否则,继续向前扫描,直到前缀和非负,形成一个满足条件的子序列。如此进行贪心即可。
     
     
     
    #include <cstdio>
    #define ll long long
    
    const int MAXN = 1e6+5;
    int a[MAXN];
    
    int main()
    {
        int n;
        while(~scanf("%d", &n)){
            for(int i = 0; i < n; ++i)
                scanf("%d", &a[i]);
            ll sum = 0;
            int ans = 0;
            for(int i = n-1; i >= 0; --i){
                sum += a[i];
                if(sum >= 0){
                    ++ans;
                    sum = 0;
                }
            }
            printf("%d
    ", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    20162330 2016-2017-2《程序设计与数据结构》课程总结
    强化学习--Policy Gradient
    59. Spiral Matrix II
    54. Spiral Matrix(剑指offer 19)
    58. Length of Last Word
    c++ string split
    神经网络反向传播,通俗理解
    大话设计模式C++ 备忘录模式
    57. Insert Interval
    c++ sort
  • 原文地址:https://www.cnblogs.com/inmoonlight/p/5733188.html
Copyright © 2011-2022 走看看