• HDU 5783 Divide the Sequence(数列划分)


    HDU 5783 Divide the Sequence(数列划分)

    Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

     

    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. 
    Alice有一个数列A,她想把A拆分出尽可能多的子串,并且每个子串的任意前缀和都大于0。
    CN
     
    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.
    多组测试用例。
    每组测试用例的第一行为一个整数n。
    下一行有n个整数A1,A2⋯An。
    1≤n≤1e6
    −10000≤A[i]≤10000
    你可以认为不存在无解的情况。
    CN

    Output - 输出
    For each test case, output an integer indicates the maximum number of sequence division.
    对于每组测试用例,输出此序列的最大划分数。
    CN
     

    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
    

    题解

      贪心,一开始看错题目了,一直想不出解法,浪费了巨多时间……
      某子串1 2 3的所有前缀为

    1
    1 2
    1 2 3
    


      显然如果出现负数的话,需要前面的正数来凑,反正无法满足条件。
      做法就是从后往前扫描,记录临时的后n项和,>=0则清空,划分数+1。
      极限数据应该是5e5 * 10000 = 5e9,单纯的int会超,需要__int64。

     

    代码 C++

     1 #include <cstdio>
     2 __int64 data[1000001];
     3 int main(){
     4     __int64 n, i, opt, tmp;
     5     while (~scanf("%I64d", &n)){
     6         for (i = opt = tmp = 0; i < n; ++i) scanf("%I64d", data + i);
     7         for (--i; ~i; --i){
     8             tmp += data[i];
     9             if (tmp >= 0) tmp = 0, ++opt;
    10         }
    11         printf("%I64d
    ", opt);
    12     }
    13     return 0;
    14 }


     


     

     

  • 相关阅读:
    The Joy of Clojure – Laziness(6.3)
    Python Decorator Closure
    Programming clojure – Recursion and Lazyseq
    Programming Clojure Unifying Data with Sequences
    SharePoint Workflow的code运行在哪个进程? w3wp.exe 还是OWSTimer.exe?
    利用PsExec提升命令行的安全级别, 绕过组策略执行命令
    WinDBG脚本入门
    记录一个SPS2010中RSS Web Part报错的问题
    User的Delegation选项卡在Active Directory Users and Computers找不到?
    修改SPS2010的Search Core Results webpart, 令其显示文档被索引了的所有属性
  • 原文地址:https://www.cnblogs.com/Simon-X/p/6001963.html
Copyright © 2020-2023  润新知