题目描述

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。

输入输出样例

输入样例#1:

4
2
3
-3
1
输出样例#1:

3

说明

【数据规模和约定】

30%的数据满足N≤20。

100%的数据满足N≤1000,|Ai|≤100000。

分组背包

屠龙宝刀点击就送

#include <ctype.h>
#include <cstdio>
void read (int &x)
{
x=;bool f=;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-') f=;
ch=getchar();
}
while(isdigit(ch))
{
x=x*+ch-'';
ch=getchar();
}
x=f?(~x)+:x;
}
int n,A[],sum[],dp[];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
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]+);
}
if(!dp[n]) printf("Impossible");
else printf("%d",dp[n]);
return ;
}

洛谷 P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…的更多相关文章

  1. 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 ...

  2. 洛谷 1569 [USACO11FEB]属牛的抗议

    [题解] 非常显然的DP,f[i]表示到第i个位置最多分成几组,f[i]=Max(f[i],f[j]+1) (j<i,sum[j]<=sum[i]) #include<cstdio& ...

  3. P1569 [USACO11FEB]属牛的抗议

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row and numbered 1..N. The cow ...

  4. 题解【洛谷P2863】 [USACO06JAN]牛的舞会The Cow Prom

    题面 题解 \(Tarjan\)板子题. 统计出大小大于\(1\)的强连通分量数量输出即可. 代码 #include <iostream> #include <cstdio> ...

  5. 洛谷P1569属牛的抗议 超级强力无敌弱化版

    P1569 [USACO11FEB]属牛的抗议Generic Cow Prote- 题目描述 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约 ...

  6. 洛谷P1522 [USACO2.4]牛的旅行 Cow Tours

    洛谷P1522 [USACO2.4]牛的旅行 Cow Tours 题意: 给出一些牧区的坐标,以及一个用邻接矩阵表示的牧区之间图.如果两个牧区之间有路存在那么这条路的长度就是两个牧区之间的欧几里得距离 ...

  7. USACO 奶牛抗议 Generic Cow Protests

    USACO 奶牛抗议 Generic Cow Protests Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望 ...

  8. 洛谷——P1821 [USACO07FEB]银牛派对Silver Cow Party

    P1821 [USACO07FEB]银牛派对Silver Cow Party 题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...

  9. 洛谷 P2986 [USACO10MAR]伟大的奶牛聚集Great Cow Gat…(树规)

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

随机推荐

  1. MVC中从Controller像View层传值

    MVC中的Controller不能直接的訪问View层中的控件,那么是怎样的将Controller中值传到View中,经常使用的有4种 ViewData: 是获取或设置视图的字典对象,它里面存放的是键 ...

  2. 织梦仿站列表页pagelist分页显示竖排,如何修改成横排?

    织梦仿站列表页pagelist分页显示竖排,如何修改成横排? 织梦列表页的分页标签是采用pagelist来进行调用的,但是很多人在调用之后会出现一个列表竖着排列的问题(横排美观度好一些),还是非常不美 ...

  3. JS处理空格

    JS处理空格 2010-10-27 11:48:32|  分类: 技术-JS |  标签:js  空格  |字号 订阅     /*删除两侧空格*/ function trim(ui){        ...

  4. 并不对劲的bzoj2038:p1494:[国家集训队]小Z的袜子

    题目大意 有\(n\)(\(n\leq5*10^4\))个数\(a_1,a_2,...,a_n\)(\(\forall i\in[1,n], 1\leq a_i\leq n\)) \(m\)(\(m\ ...

  5. Splay基本操作

    我们以一道题来引入吧! 传送门 题目说的很清楚,我们的数据结构要支持:插入x数,删除x数,查询数的排名和排名为x的数,求一个数前驱后继. 似乎用啥现有的数据结构都很难做到在O(nlogn)的复杂度中把 ...

  6. hdu 1719

    Friend Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  7. bzoj3090

    树形dp 有一个比较明显的dp状态是dp[i][j]表示当前i节点的子树已经满足且i剩下j元钱的最小操作次数,这样复杂度比较高状态数已经有O(n*x)的了,转移再来x,肯定不行. 我们考虑把状态和dp ...

  8. JSP指令--include指令(静态包含)

    转自:https://blog.csdn.net/chentiefeng521/article/details/51802319 include指令         include指令是文件加载指令, ...

  9. LUR和缺页次数

    缺页:缺页中断就是要访问的页不在主存,需要操作系统将其调入主存后再进行访问. LRU(Least recently used)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问 ...

  10. bzoj 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头【瞎搞】

    某种意义上真毒瘤?我没看懂题啊...于是看了题解 就是筛约数的那种方法,复杂度调和级数保证O(nlogn) 所以这题啥意思啊 #include<iostream> #include< ...