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#学习笔记_07_数组
07_数组 数组的声明与实例化 名词解释 数组:数组是一个容器,用来存储一系列相兼容的数据类型的变量: 实例化:声明一个数组,并且赋初始值: 数组长度:就是数组的容量,表示这个数组可以存储多少个数据: ...
- 3 numpy模块
Numpy 什么是Numpy:Numeric Python Numpy模块是Python的一种开源的数值计算扩展. 1 一个强大的N维数组对象Array ...
- 大专生自学Python到找到工作的心得
先做个自我介绍,我13年考上一所很烂专科民办的学校,学的是生物专业,具体的学校名称我就不说出来献丑了.13年我就辍学了,我在那样的学校,一年学费要1万多,但是根本没有人学习,我实在看不到希望,我就退学 ...
- pip操作以及window和虚拟机中为pip更换一个国内的镜像源的方法
前言 在学习PyQt5的过程中,参考王硕和孙洋洋的PyQt5快速开发与实战中,看到的关于Python开发技巧与实战,觉得挺好的 所以将其摘抄了下来方便阅读.之后还有一个关于更换pip镜像源的方法,方便 ...
- poj3233 题解 矩阵乘法 矩阵快速幂
题意:求S = A + A2 + A3 + … + Ak.(mod m) 这道题很明显可以用矩阵乘法,但是这道题的矩阵是分块矩阵, 分块矩阵概念如下:当一个矩阵A中的单位元素aij不是一个数值而是一个 ...
- nyoj_8_一种排序_201311251238
一种排序 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编 ...
- springmvc 解析xml数据
springmvc 解析xml数据 http://blog.csdn.net/zhi_jun/article/details/37925475
- Codeforces Round #281 (Div. 2) A. Vasya and Football 暴力
A. Vasya and Football Vasya has started watching football games. He has learned that for some foul ...
- 查看及改动Oracle编码格式方法
首先查看oracle数据库的编码 SQL> select * from nls_database_parameters where parameter ='NLS_CHARACTERSET ...
- oc58--Category注意事项
// // main.m // Category注意事项 #import <Foundation/Foundation.h> #import "Person+NJ.h" ...