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;
    }
    

      

  • 相关阅读:
    【总结】搜索
    【luogu】p2296 寻找道路
    【luogu】p2058 海港
    【总结】二叉搜索树
    【总结】线段树
    【总结】矩阵快速幂
    【笔记】很基础的数论知识
    【总结】扩展欧几里得算法
    【总结】二分查找
    【高精度乘法】例1.4 课本185页
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5732118.html
Copyright © 2011-2022 走看看