洛谷 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。

 

输入输出样例

输入样例#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=0;bool f=0;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-') f=1;
        ch=getchar();
    }
    while(isdigit(ch))
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    x=f?(~x)+1:x;
}
int n,A[100005],sum[100005],dp[100005]; 
int max(int a,int b)
{
    return a>b?a:b;
}
int main()
{
    read(n);
    for(int i=1;i<=n;i++)
    {
        read(A[i]);sum[i]=sum[i-1]+A[i];
        if(sum[i]>=0) dp[i]=1;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;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");
    else printf("%d",dp[n]);
    return 0;
}

 

转载于:https://www.cnblogs.com/ruojisun/p/6796267.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值