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

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

    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

    题意:

    给定一串数字分成若干份,使任意份的任意前缀和都不为负。

    从后向前遍历贪心。

    附AC代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 
     8 int a[1000010];
     9 
    10 int main(){
    11     int n;
    12     while(~scanf("%d",&n)){
    13         int sum=n-1;
    14         for(int i=0;i<n;i++){
    15             scanf("%d",&a[i]);
    16         }
    17         int t=0;
    18         while(sum>=0){
    19             long long ans=0;
    20             ans+=a[sum];
    21             while(ans<0){
    22                 sum--;
    23                 ans+=a[sum];
    24             }
    25             t++;
    26             sum--;
    27         }
    28         printf("%I64d
    ",t);
    29 }
    30     return 0;
    31 }
  • 相关阅读:
    foreach的使用(c#)
    ssl详解
    c#的html格式化输出(看php10012视频时想到的)
    PHP句法规则详解(转载)
    C#中Bitmap类实现对图像操作的一些方法(Asp.net(C#)放大缩小图片尺寸)
    c# 窗体最小化到托盘
    C# 中的委托和事件
    C# StringBuilder和String浅析
    c#值类型和引用类型
    后台关闭页面
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/5745543.html
Copyright © 2011-2022 走看看