前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >P1569 [USACO11FEB]属牛的抗议Generic Cow Prote…

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

作者头像
attack
发布2018-04-12 15:28:57
6610
发布2018-04-12 15:28:57
举报

题目描述

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:

代码语言:javascript
复制
(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:

代码语言:javascript
复制
4
2
3
-3
1

输出样例#1:

代码语言:javascript
复制
3

说明

【数据规模和约定】

30%的数据满足N≤20

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

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

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

if(dp[j]>0&&sum[i]-sum[j]>=0)

就说明他们两个可以不在一个分组里面

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 using namespace std;
 7 void read(int &n)
 8 {
 9     char c='+';int x=0;bool flag=0;
10     while(c<'0'||c>'9')
11     {c=getchar();if(c=='-')flag=1;}
12     while(c>='0'&&c<='9')
13     {x=x*10+(c-48);c=getchar();}
14     flag==1?n=-x:n=x;
15 }
16 int n,m;
17 int a[10001];
18 int dp[10001];
19 int sum[10001];
20 int main()
21 {
22     int i,j,k;
23     read(n);
24     for(int i=1;i<=n;i++)
25     {
26         read(a[i]);
27         sum[i]=sum[i-1]+a[i];
28         if(sum[i]>=0)
29             dp[i]=1;
30     }    
31        for(int i=1;i<=n;i++)
32            for(int j=1;j<i;j++)
33                if(dp[j]>0&&sum[i]-sum[j]>=0)
34                dp[i]=max(dp[i],dp[j]+1);
35     dp[n]==0?printf("Impossible"):printf("%d",dp[n]);    
36     return 0;
37 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 输入输出格式
  • 输入输出样例
  • 说明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档