P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…
题目描述
Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cows are conducting another one of their strange protests, so each cow i is holding up a sign with an integer A_i (-10,000 <= A_i <= 10,000).
FJ knows the mob of cows will behave if they are properly grouped and thus would like to arrange the cows into one or more contiguous groups so that every cow is in exactly one group and that every group has a nonnegative sum.
Help him count the number of ways he can do this, modulo 1,000,000,009.
By way of example, if N = 4 and the cows' signs are 2, 3, -3, and 1, then the following are the only four valid ways of arranging the cows:
(2 3 -3 1)
(2 3 -3) (1)
(2) (3 -3 1)
(2) (3 -3) (1)
Note that this example demonstrates the rule for counting different orders of the arrangements.
约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动。第i头奶牛的理智度 为Ai,Ai可能是负数。约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组,每个小组内的奶牛的理智度总和都要大于零。由于奶牛是按直线排列的,所以 一个小组内的奶牛位置必须是连续的。 请帮助约翰计算一下,最多分成几组。
输入输出格式
输入格式:
第1行包含1个数N,代表奶牛的数目。
第2至N+1行每行1个整数Ai。
输出格式:
输出文件有且仅有一行,包含1个正整数即为最多组数。
若无法满足分组条件,则输出Impossible。
输入输出样例
4
2
3
-3
1
3
说明
【数据规模和约定】
30%的数据满足N≤20。
100%的数据满足N≤1000,|Ai|≤100000。
一开始想到用前缀和维护了,但是,还是不自信啊,,
题解里面用到了一个很巧妙的东西就是
if(dp[j]>0&&sum[i]-sum[j]>=0)
就说明他们两个可以不在一个分组里面
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
void read(int &n)
{
char c='+';int x=;bool flag=;
while(c<''||c>'')
{c=getchar();if(c=='-')flag=;}
while(c>=''&&c<='')
{x=x*+(c-);c=getchar();}
flag==?n=-x:n=x;
}
int n,m;
int a[];
int dp[];
int sum[];
int main()
{
int i,j,k;
read(n);
for(int i=;i<=n;i++)
{
read(a[i]);
sum[i]=sum[i-]+a[i];
if(sum[i]>=)
dp[i]=;
}
for(int i=;i<=n;i++)
for(int j=;j<i;j++)
if(dp[j]>&&sum[i]-sum[j]>=)
dp[i]=max(dp[i],dp[j]+);
dp[n]==?printf("Impossible"):printf("%d",dp[n]);
return ;
}
P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…的更多相关文章
- 洛谷 P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cow ...
- P1569 [USACO11FEB]属牛的抗议
题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cow ...
- 洛谷 1569 [USACO11FEB]属牛的抗议
[题解] 非常显然的DP,f[i]表示到第i个位置最多分成几组,f[i]=Max(f[i],f[j]+1) (j<i,sum[j]<=sum[i]) #include<cstdio& ...
- 洛谷P1569属牛的抗议 超级强力无敌弱化版
P1569 [USACO11FEB]属牛的抗议Generic Cow Prote- 题目描述 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约 ...
- USACO 奶牛抗议 Generic Cow Protests
USACO 奶牛抗议 Generic Cow Protests Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望 ...
- 洛谷 2344 奶牛抗议 Generic Cow Protests, 2011 Feb
[题解] 我们可以轻松想到朴素的状态转移方程,但直接这样做是n^2的.所以我们考虑采用树状数组优化.写法跟求逆序对很相似,即对前缀和离散化之后开一个权值树状数组,每次f[i]+=query(sum[i ...
- P2863 [USACO06JAN]牛的舞会The Cow Prom
洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's ...
- luoguP2863 [USACO06JAN]牛的舞会The Cow Prom
P2863 [USACO06JAN]牛的舞会The Cow Prom 123通过 221提交 题目提供者 洛谷OnlineJudge 标签 USACO 2006 云端 难度 普及+/提高 时空限制 1 ...
- BZOJ2274: [Usaco2011 Feb]Generic Cow Protests
2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 196 Solve ...
随机推荐
- 7-13 航空公司VIP客户查询 (25 分)
题意: 思路: 读完题目之后的第一思路就是用map将客户的id(string类型)与里程road(int类型)形成映射,然后直接用id查找添加里程或输出里程.但是400ms的限制妥妥的超时了.然后意识 ...
- 原来PHP对象比数组用更少的内存
一直以为php的数组更节省内存,从来没有测试过,今天因为要读取一个大配置文件作为pool.做了一次测试: 得出结论是 使用对象保存数据更好,花费的内存是数组array的1/4. 测试代码 class ...
- 编写who命令
第一个版本: /* who1.c - a first version of the who program * open, read UTMP file, and show results. */ # ...
- Marshal.ReleaseComObject() vs. Marshal.FinalReleaseComObject()
很简单,不翻译了. If you are using COM components on your .NET code, you might be already aware of the Marsh ...
- vue轮播插件vue-awesome-swiper
https://surmon-china.github.io/vue-awesome-swiper/ 第一步安装 npm install vue-awesome-swiper --save 第二部在m ...
- 【codeforces 779E】Bitwise Formula
[题目链接]:http://codeforces.com/contest/779/problem/E [题意] 给你n个长度为m的二进制数 (有一些是通过位运算操作两个数的形式给出); 然后有一个未知 ...
- python库文件路径
python中import语句导入库文件路径可通过sys.path查看.写一个简单的小程序: import sys print sys.path 运行它,本机上得到的结果如下: ['', '/usr/ ...
- hdu_1060_Leftmost Digit_201311071827-2
Leftmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 最小堆的两种实现及其STL代码
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> boo ...
- propagation属性的7个传播行为
关于propagation属性的7个传播行为 REQUIRED:指定当前方法必需在事务环境中运行,如果当前有事务环境就加入当前正在执行的事务环境,如果当前没有事务,就新建一个事务.这是默认值.即有事务 ...