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 ...
随机推荐
- C++实现双人枪战游戏
//单机版枪战游戏,喜欢就拿走,把赞留下//by floatiy #include<iostream> #include<cstdio> #include<windows ...
- [ZJOJ] 5772【NOIP2008模拟】今天你AK了吗
Description AK:All kill“你为什么没背书?”“没有为什么,我就是没背书.”“……我去年买了个表,G—U—N!”头铁王InFleaKing把背书的时间都拿去列排列了......n= ...
- String s="a"+"b"+"c"+"d";创建了几个对象?
对于如下代码: package reviewTest; /** * @ClassName: StringTest * @Description: 测试String的字符串相加优化 * @author ...
- 第六节:numpy之数组拼接
- dubbo服务telnet命令的使用
转自:https://www.cnblogs.com/feiqihang/p/4387330.html dubbo服务发布之后,我们可以利用telnet命令进行调试.管理.Dubbo2.0.5以上版本 ...
- 【codeforces 760D】Travel Card
[题目链接]:http://codeforces.com/contest/760/problem/D [题意] 去旅行,有3种类型的乘车票; 第一种:只能旅行一次20元 第二种:按时间计算,90分钟内 ...
- Maven学习总结(3)——使用Maven构建项目
Maven学习总结(三)--使用Maven构建项目 maven作为一个高度自动化构建工具,本身提供了构建项目的功能,下面就来体验一下使用maven构建项目的过程. 一.构建Jave项目 1.1.创建J ...
- Codeforces 263C. Appleman and Toastman
C. Appleman and Toastman time limit per test 2 seconds memory limit per test 256 megabytes input ...
- [bzoj1468][poj1741]Tree[点分治]
可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...
- linux服务器中不支持soap及bcmul函数的结局方法
新的程序里用了webserice接口,部到服务器,先是提示:bcmul() 函数不可用,网上搜索一番,得知这是php的高精度函数,需要在编译php的时候加入此模块,于是在编译脚本里增添 “–enabl ...