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 begin with an integer n in a single line.
The next line contains n

integers A_{1},A_{2}\cdots A_{n}

.
1 \leq n \leq 1e6

-10000 \leq A[i] \leq 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

就是给你一个序列,让你求最多能分成多少段连续子序列,并且每段子序列的前缀个都得是大于等于0的;思路就是从后往前遍历,要想分的最多那么有整数直接,当成一个数列就行了,有负数往前递加直到和为零。这里特别注意,数据要用long long要不然会溢出的

#include <iostream>#include <algorithm>#include <stdio.h>#include <string.h>#define N 1000005#define M (1<<15)+5#define INF 0x3f3f3fusing namespace std;long long n,a[N],sum;int main(){	//freopen("in.txt","r",stdin);	while(scanf("%lld",&n)!=EOF)	{		memset(a,0,sizeof(a));		for(int i=0;i<n;i++)		{			scanf("%lld",&a[i]);		}			long long cur=0;		for(int i=n-1;i>=0;i--)//这个是首位枚举		{			if(a[i]>=0)				cur++;			else			{				sum=0;				for(;i>=0;i--)				{					sum+=a[i];					if(sum>=0)					{						cur++;						break;					}					}				}		}			printf("%lld\n",cur);	}    return 0;}
时间: 2024-12-19 22:25:20

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

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 (贪心)

把长度为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

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

hdu 4893 Wow! Such Sequence!(线段树功能:单点更新,区间更新相邻较小斐波那契数)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 --------------------------------------------------------------------------------------------------------------------------------------------

HDU 4893 Wow! Such Sequence! (线段树)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 838    Accepted Submission(s): 245 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

HDU Wow! 4893 Such Sequence!(线段树)

HDU 4893 Wow! Such Sequence! 题目链接 题意:给定一个序列,3种操作,单点添加值,查询区间和,把区间和变成最接近的婓波那契数 思路:线段树,就是第三个操作麻烦,就在结点添加一个值,标记它区间是不是都是婓波那契数了,然后修改区间的时候,如果区间是了就不用修改,如果不是就继续往后一层推即可 代码: #include <cstdio> #include <cstring> #include <cstdlib> #define lson(x) ((x

HDU 5063 Operation the Sequence(暴力)

HDU 5063 Operation the Sequence 题目链接 把操作存下来,由于只有50个操作,所以每次把操作逆回去运行一遍,就能求出在原来的数列中的位置,输出即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int N = 100005; const ll MOD = 100000

HDU - 1711 A - Number Sequence(kmp

HDU - 1711 A - Number Sequence Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[