zoukankan      html  css  js  c++  java
  • HDU 5783 Divide the Sequence (训练题002 B)

    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 A_{1},A_{2}cdots A_{n} .
    1 leq n leq 1e6
    -10000 leq A[i] leq 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

    /*就是给你一个序列,让你求最多能分成多少段连续子序列,并且每段子序列的前缀个都得是大于等于0的;
    思路就是从后往前遍历,要想分的最多那么有整数直接,当成一个数列就行了,有负数往前递加直到和为零。这里特别注意,数据要用long long要不然会溢出的
    */
    
    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    #include <string.h>
    #define N 1000005
    #define M (1<<15)+5
    #define INF 0x3f3f3f
    using namespace std;
    long long n,a[N],sum;
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%lld",&n)!=EOF)
        {
            memset(a,0,sizeof(a));
            for(int i=0;i<n;i++)
            {
                scanf("%lld",&a[i]);
            }    
            long long cur=0;
            for(int i=n-1;i>=0;i--)//这个是首位枚举
            {
                if(a[i]>=0)
                    cur++;
                else
                {
                    sum=0;
                    for(;i>=0;i--)
                    {
                        sum+=a[i];
                        if(sum>=0)
                        {
                            cur++;
                            break;
                        }    
                    }    
                }
            }
                printf("%lld
    ",cur);
        }
        return 0;
    }
     
  • 相关阅读:
    键盘弹出与隐藏对TextView的影响
    iOS9 警告框
    计时器的写法
    iOS提交被拒
    新生活
    批量删除wps文档里的回车符的方法!WPS使用技巧分享!
    学习笔记计划
    监控服务器的注册及登陆并邮件通知的代码(go / python)
    Python调用C代码
    导入用户到Discuz论坛
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5738779.html
Copyright © 2011-2022 走看看