HDU-5783 Divide the Sequence(贪心)

题目大意:给一个整数序列,将其划分成若干个子连续序列,使其每个子序列的前缀和不为负。求最大的划分个数。

题目分析:从后往做累加计算,如果不为负,则计数加一,累加和清0。否则,一直往前扫描。如果最终的和为负,答案为0,否则为计数结果。

代码如下:

# include<iostream>
# include<cstdio>
# include<algorithm>
using namespace std;
# define LL long long

const int N=1000000;

int a[N+5];

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;++i)
            scanf("%d",a+i);
        int ans=0;
        LL sum=0;
        for(int i=n-1;i>=0;--i){
            sum+=(LL)a[i];
            if(sum>=0){
                ++ans;
                sum=0;
            }
        }
        if(sum<0) cout<<0<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}

  

时间: 2024-08-28 04:14:20

HDU-5783 Divide the Sequence(贪心)的相关文章

HDU 5783 Divide the Sequence (贪心)

把长度为n的序列分成尽量多的连续段,使得每一段的每个前缀和都不小于0.保证有解. 从后往前贪心分段即可.大于等于0的为一段,遇到负数就一直相加到非负为止!(注意精度问题 用long long) #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const LL N=1000010; LL a[N]; int main() { LL n,i; while(~scanf

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, satisfy

【贪心】HDU 5783 Divide the Sequence

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5783 题目大意: 把一个N个数的数列拆成若干段,保证每一段的前缀和都非负,求最多能拆成多少段. 题目思路: [贪心] 一开始题目看错了看成每一段内和非负..DPWA了好久. 默认答案是n,从后往前找负数,找到一个负数就一直把它往前合并直到和值非负,这样这个区间的前缀和就一定非负,扣除合并的区间大小即可. 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h>

HDU 5783 Divide the Sequence ()

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

HDU 5783 Divide the Sequence

开个栈弹一下. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

hdu 4915 Parenthese sequence (贪心+模拟)

题目大意: 一个序列中有左括号和右括号,还有问号,问号可以任意转换成左右括号. 问这个序列有多少种情况的转变使得这个序列变成合法的括号匹配序列. 思路分析: 首先我们分析一下,如何使得一个序列是合法的括号匹配序列. 我们很容易想到的是用栈模拟匹配过程. 当遇到左括号就进栈,当遇到右括号就让栈顶的左括号出栈. 那么在模拟的过程中,造成这个序列的不合法的原因只有当右括号来的时候,此时的栈已经为空. 这里补充一句,一旦一个序列给定,那么这里面的问号有多少变成左括号,多少变成右括号,是一定的. 看完以上

HDU 1051 并查集+贪心

Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11694    Accepted Submission(s): 4837 Problem Description There is a pile of n wooden sticks. The length and weight of each stick ar

hdu 4825 Xor Sum(trie+贪心)

hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #define CLR(a,b) memset((a)

【贪心专题】HDU 1009 FatMouse&#39; Trade (贪心选取)

链接:click here~~ 题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物.有N个房间,每个房间里面都有食物.你可以得到J[i]单位的食物,但你需要付出F[i]单位的的猫食. 计算M磅猫食可以获得最多食物的重量. [解题思路]贪心算法,求最优解.将J[i]/F[i]的值从大到小排列,每次取最大的,局部最优,达到全局最优,从而获得最大值. 代码: // 贪心策略,优先选择投资最大的房间,每选择一次,交换次数依次减少,最后的次数用于价值最小的 //注意精度转化:1.0*(int