bzoj 2274 [Usaco2011 Feb]Generic Cow Protests

Description
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个数,问有几种划分方案(不能改变数的位置),使得每组中数的和大于等于0。输出方案数除以 1000000009的余数。

Input
* Line 1: A single integer: N
* Lines 2..N + 1: Line i + 1 contains a single integer: A_i
Output
* Line 1: A single integer, the number of arrangements modulo 1,000,000,009.

Sample Input
4
2
3
-3
1
Smple Output
4

Solution

可得简单dp如下

    cin>>n;
    for(int i=1;i<=n;i++) 
    {
        scanf("%d",&a[i]);
        s[i]=s[i-1]+a[i];
    }
    f[0]=1;
    for(int i=1;i<=n;i++) 
    {
        for(int j=0;j<i;j++) 
        if(s[i]-s[j]>=0) f[i]=(f[i]+f[j])%mod;
    }
    cout<<f[n];

完了这是n^2的怎么办
其实很多题都是像这样的套路,关键就是对dp条件式的转化
s[i]-s[j]>=0即s[i]>=s[j]
我们可以离散化s数组,然后用线段树来求和。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
#define p1 id<<1
#define p2 id<<1^1
using namespace std;
const int mod=1000000009;
int n,x;
int a[100005],f[100005],tree[400005];
ll s[100005];
struct ty
{
    ll v;
    int id;
}q[100005];
bool cmp(ty x,ty y)
{
    return x.v<y.v;
}
int query(int id,int l,int r,int x,int y)
{
    if(x<=l&&r<=y) return tree[id];
    int mid=(l+r)/2;
    if(y<=mid) return query(p1,l,mid,x,y);
    else
    if(x>mid) return query(p2,mid+1,r,x,y);
    else return (query(p1,l,mid,x,mid)+query(p2,mid+1,r,mid+1,y))%mod;
}
void update(int id,int l,int r,int x,int y)
{
    if(l==r) 
    {
        tree[id]=(tree[id]+y)%mod;
        return;
    }
    int mid=(l+r)/2;
    if(x<=mid) update(p1,l,mid,x,y); else update(p2,mid+1,r,x,y);
    tree[id]=(tree[p1]+tree[p2])%mod;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) 
    {
        scanf("%d",&a[i]);
        s[i]=s[i-1]+a[i];
    }
    for(int i=0;i<=n;i++) 
    {
        q[i].v=s[i];
        q[i].id=i;
    }
    sort(q,q+n+1,cmp);
    x=1;
    s[q[0].id]=x;
    for(int i=1;i<=n;i++) 
    {
        if(q[i].v!=q[i-1].v) x++;
        s[q[i].id]=x;
    }
    f[0]=1;
    update(1,1,x,s[0],f[0]);
    for(int i=1;i<=n;i++) 
    {
        f[i]=query(1,1,x,1,s[i]);
        update(1,1,x,s[i],f[i]);
    }
    cout<<f[n];
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值