zoukankan      html  css  js  c++  java
  • HDU 5783 Divide the Sequence (贪心)

    Divide the Sequence

    题目链接:

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

    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,A2⋯An.
    1≤n≤1e6
    −10000≤A[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

    Source

    2016 Multi-University Training Contest 5


    ##题意: 将数串A分解成尽可能多的字串,使得每个字串的任一前缀和都不小于零.
    ##题解: 贪心. 从后往前遍历,每个和为零的地方都可以分割一次. 注意用longlong.
    ##代码: ``` cpp #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 1010000 #define mod 100000007 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std;

    int n;
    LL num[maxn];

    int main(int argc, char const *argv[])
    {
    //IN;

    //int t; cin >> t;
    while(scanf("%d", &n) != EOF)
    {
        for(int i=1; i<=n; i++)
            scanf("%I64d", &num[i]);
    
        LL cur = 0;
        int ans = 0;
        for(int i=n; i>=1; i--) {
            cur += num[i];
            if(cur >=0) {
                ans++;
                cur = 0;
            }
        }
    
        printf("%d
    ", ans);
    }
    
    return 0;
    

    }

  • 相关阅读:
    OSG在VS2008下的配置安装
    MFC编译Freetype2.3.7
    在VS中编译Opencascade6.6.0
    MFC 双缓存绘图
    SDI在自定义的工具栏上添加下拉控件
    DECLARE_MESSAGE_MAP用法
    64位电脑上安装MySQL进行MFC开发的相关问题
    VS2005+VTK读入点云文件
    VS2005编译VTK5.10.1
    HTML入门:Tag学习
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5730736.html
Copyright © 2011-2022 走看看