【贪心】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>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define mod 1000000007
26 #define MAX 0x7f7f7f7f
27 #define PI 3.14159265358979323
28 #define N 1000004
29 using namespace std;
30 typedef long long LL;
31 int cas,cass;
32 int n,m,lll,ans;
33 LL a[N];
34 int main()
35 {
36     #ifndef ONLINE_JUDGE
37 //    freopen("1.txt","r",stdin);
38 //    freopen("2.txt","w",stdout);
39     #endif
40     int i,j,k;
41 //    for(scanf("%d",&cas);cas;cas--)
42 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
43 //    while(~scanf("%s",s+1))
44     while(~scanf("%d",&n))
45     {
46         for(i=1;i<=n;i++)
47             scanf("%lld",&a[i]);
48         ans=n;
49         for(i=n;i;i--)
50         {
51             if(a[i]<0)
52             {
53                 j=i;
54                 while(a[j]<0)a[j]+=a[--i];
55                 ans-=j-i;
56             }
57         }
58         printf("%d\n",ans);
59     }
60     return 0;
61 }
62 /*
63 //
64
65 //
66 */

时间: 2024-10-22 20:04:01

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

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

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 1009 FatMouse&#39; Trade

题意 有n种物品,每一种需要不同的消费,现在手里有m块钱,求问最多可以买多少 分析 贪心 把每一种物品的价格算出来,然后sort一下,按照价格从便宜到贵排序,能买多少买多少,买买买! Accepted Code 1 /* 2 PROBLEM:hdu1009 3 AUTHER:Nicole Lam 4 MEMO:贪心 5 */ 6 7 #include<cstdio> 8 #include<algorithm> 9 using namespace std; 10 11 12 stru

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 4893 Wow! Such Sequence!(线段树)

题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 间l到r区间上的所以数变成最近的斐波那契数,相等的话取向下取. 解题思路:线段树,对于每个节点新增一个bool表示该节点以下的位置是否都是斐波那契数. #include <cstdio> #include <cstring> #include <cstdlib> #include <algor

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

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

HDU 4893 Wow! Such Sequence! 水线段树

思路: 线段树走起.. 写完这题就退役T^T 单点更新的时候直接找到这个点的最近fib,然后维护当前和 和 fib的和 #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<algorithm> #include<queue> #include<map> #include<set> #include&l