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;
    }
     
  • 相关阅读:
    (个人题目)作业 题解
    P2618 数字工程
    P6394 樱花,还有你
    USACO08FEB Making the Grade G
    USACO13NOV Pogo-Cow S
    CSP2019 树上的数
    JSOI2018 潜入行动
    NOIP2017 宝藏
    SNOI2017 炸弹
    【洛谷】【最小生成树】P1195 口袋的天空
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5738779.html
Copyright © 2011-2022 走看看