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

    题目链接:

    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.
     
    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代码:

    /************************************************
    ┆  ┏┓   ┏┓ ┆   
    ┆┏┛┻━━━┛┻┓ ┆
    ┆┃       ┃ ┆
    ┆┃   ━   ┃ ┆
    ┆┃ ┳┛ ┗┳ ┃ ┆
    ┆┃       ┃ ┆ 
    ┆┃   ┻   ┃ ┆
    ┆┗━┓    ┏━┛ ┆
    ┆  ┃    ┃  ┆      
    ┆  ┃    ┗━━━┓ ┆
    ┆  ┃  AC代马   ┣┓┆
    ┆  ┃           ┏┛┆
    ┆  ┗┓┓┏━┳┓┏┛ ┆
    ┆   ┃┫┫ ┃┫┫ ┆
    ┆   ┗┻┛ ┗┻┛ ┆      
    ************************************************ */ 
     
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
     
    using namespace std;
     
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
     
    typedef  long long LL;
     
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
     
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=1e9;
    const int N=1e6+10;
    const int maxn=2e3+14;
    const double eps=1e-8;
    
    int a[N];
    LL sum[N];
    
    inline void solve()
    {
    
    }
    
    int main()
    {      
            int n;
            while(cin>>n)
            {
                For(i,1,n)read(a[i]),sum[i]=sum[i-1]+a[i];
                int ans=0,cur=n,i=n-1;
                LL cursum=sum[cur];
                while(cur)
                {
                    if(sum[i]<=cursum)
                    {
                        ans++;
                        cur=i;
                        cursum=sum[cur];
                    }
                    i--;
                }
                printf("%d
    ",ans);
            }
            return 0;
    }
    

      

  • 相关阅读:
    iOS企业证书开发的APP证书过期时间监控
    事件冒泡,事件捕获
    倒计时
    获取多个div,点击第几个,显示第几个
    js继承
    javascript基础知识总结
    大型web系统高效应用方法(转载)
    数据库(内联,外联,交叉联)
    .net零碎基础知识点不完全小结
    C#的内存管理:堆、栈、托管堆与指针(转)
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5732118.html
Copyright © 2011-2022 走看看