题目描述

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。

区间DP入门题。。。

用 sum 记录 前缀和 。

设 dp[i] 表示到第i个数时,能分成的最大组数。

则:枚举j:1~i,dp[i]=max{ dp[j]+1,dp[i] }

注意:sum[n]<0时可以直接输出Impossible

附代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 1010
using namespace std;
int n,a[MAXN],sum[MAXN],dp[MAXN];
inline int read(){int date=0,w=1;char c=0;while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}return date*w;
}
int main(){n=read();sum[0]=0;for(int i=1;i<=n;i++){a[i]=read();sum[i]=sum[i-1]+a[i];if(sum[i]>=0)dp[i]=1;}if(sum[n]<0){printf("Impossible\n");return 0;}for(int i=1;i<=n;i++)for(int j=1;j<i;j++)if(dp[j]>0&&sum[i]-sum[j]>=0)dp[i]=max(dp[i],dp[j]+1);if(!dp[n])printf("Impossible\n");else printf("%d\n",dp[n]);return 0;
}

洛谷P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…相关推荐

  1. 洛谷 P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

    P1569 [USACO11FEB]属牛的抗议Generic Cow Prote- 题目描述 Farmer John's N (1 <= N <= 100,000) cows are li ...

  2. P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

    P1569 [USACO11FEB]属牛的抗议Generic Cow Prote- 题目描述 Farmer John's N (1 <= N <= 100,000) cows are li ...

  3. 信息学奥赛一本通 1343:【例4-2】牛的旅行 | 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours

    [题目链接] ybt 1343:[例4-2]牛的旅行 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours [题目考点] 1. 图论 最短路径 Floyd算法 Floyd算法时间复杂度 ...

  4. 洛谷 P3014 [USACO11FEB]牛线Cow Line

    P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) ...

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

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  6. 洛谷P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

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

    题目描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the b ...

  8. 洛谷——P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

  9. 洛谷 P2862 [USACO06JAN]把牛Corral the Cows

    P2862 [USACO06JAN]把牛Corral the Cows 题目描述 Farmer John wishes to build a corral for his cows. Being fi ...

最新文章

  1. 数据挖掘-数据清理过程
  2. 如何为WCF应用添加X509证书和安全验证
  3. 2021年MathorCupD题思路
  4. 人人可以理解的区块链100问——比特币可以用于支付吗?
  5. linux期末作业设计,linux作业与项目设计
  6. Entity Framework Core 之简单介绍
  7. python生成器用法_理解python中生成器用法
  8. libjpeg-turbo在Windows VS2010下的编译
  9. RMI 异常 no security manager: RMI class loader disabled
  10. oracle空值减去一个数_Oracle 空值(null)有关的函数
  11. Javascript游戏,街头霸王
  12. postgresql将数据从一个表内容插入到另一个表_关系型数据库管理系统openGauss 1.0.1版本发布...
  13. python做一个爬虫要用到什么软件_python实现简单爬虫功能
  14. java jdk生成安卓app证书
  15. 全球IP将告罄,美国已摇号限购
  16. 1002:方便记忆的电话号码
  17. ArcGIS中相对高程的提取
  18. 一种改进的教与学优化算法
  19. CPU Cycle(CPU 周期)、Instruction Cycle(指令周期)、Clock Cycle(时钟周期)
  20. 解决MySQl卸载卸不干净问题

热门文章

  1. android 三维软件 cad,CAD实例教程:快速设计呆萌的安卓机器人
  2. 五款最棒的Go语言开发工具?
  3. 声源定位与stm32示例
  4. 笔记本未启用无线服务器,WiFi无线网络提示未启用DHCP无法上网的解决方法
  5. Oracle函数之LAG函数
  6. 《从零开始的RPG游戏制作教程》第二期:让勇者和怪物登场
  7. uniapp点击回复弹起键盘输入
  8. linux rz 快捷上传包或文件
  9. xshell文件传输乱码_在Xshell中使用rz命令上传文件出现乱码且文件无法删除的解决办法...
  10. Excel软件的使用指南