题目描述

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。

一开始想到用前缀和维护了,但是,还是不自信啊,,

题解里面用到了一个很巧妙的东西就是

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 ;
}

最新文章

  1. 【leetcode】Path Sum II
  2. Android动画translate坐标图
  3. Scala的Actor模式 &amp; Akka框架
  4. python with as用法
  5. ISBN号码
  6. 智能指针(三):unique_ptr使用简介
  7. java_DAO类基本设计
  8. linux删除数据文件无备份恢复
  9. ios8 关于UIAlertController 代替UIActionsheet
  10. ios中点语法、property跟synthesize用法
  11. 基于TypeScript的FineUIMvc组件式开发(开头篇)
  12. thinkphp介绍及访问方式
  13. Spark Kudu 结合
  14. Flink部署-standalone模式
  15. MongoDB慢查询性能分析
  16. redis设置密码
  17. linux书籍
  18. 【BZOJ】【3673】可持久化并查集 &amp; 【3674】可持久化并查集加强版
  19. padding Oracle attack(填充Oracle攻击)
  20. 《面向对象的JavaScript》读书笔记

热门文章

  1. Problem 56
  2. python运算符及优先级
  3. 单例模式的python实现
  4. 平衡树前置——BST
  5. 【Codeforces 158A】Next Round
  6. [USACO16OPEN]关闭农场Closing the Farm(洛谷 3144)
  7. nyoj_18_The Triangle_201312071533
  8. 贪心算法 Heidi and Library (easy)
  9. mongodb之存储引擎
  10. 【Android】开发优化之——调优工具:TrackView,Method Profiling