[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

Sample Output

4


本题很容易想到一个N^2 DP,即 \[ f(i)=\sum_{j=1}^{i-1} f(j),(sum[i]-sum[j]>=0)\] 不过肯定会T就是了。我们考虑每次转移只考虑到sum[i]与sum[j]的大小关系,于是我们只要将前缀和离散化一下然后丢到树状数组里处理下就好了

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lowbit(x) ((x)&(-x))
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<'0'||ch>'9';ch=getchar())    if (ch=='-')    f=-1;
    for (;ch>='0'&&ch<='9';ch=getchar())  x=(x<<1)+(x<<3)+ch-'0';
    return x*f;
}
inline void print(int x){
    if (x>=10)     print(x/10);
    putchar(x%10+'0');
}
const int N=1e5,mod=1e9+9;
int tree[N+10],sum[N+10],f[N+10];
int n,T,ans;
struct AC{
    int x,ID;
    void join(int a,int b){x=a,ID=b;}
    bool operator <(const AC &a)const{return x!=a.x?x<a.x:ID<a.ID;}
}A[N+10];
void insert(int x,int v){for (;x<=n;x+=lowbit(x))    tree[x]=(tree[x]+v)%mod;}
int query(int x){
    int res=0;
    for (;x;x-=lowbit(x))   res=(res+tree[x])%mod;
    return res;
}
int main(){
    n=read();
    for (int i=1;i<=n;i++)   A[i].join(A[i-1].x+read(),i),f[i]=(A[i].x>=0);
    sort(A+1,A+1+n);
    for (int i=1;i<=n;i++)   sum[A[i].ID]=i;
    for (int i=1;i<=n;i++)   insert(sum[i],f[i]=(f[i]+query(sum[i]))%mod);
    printf("%d\n",f[n]);
    return 0;
}

转载于:https://www.cnblogs.com/Wolfycz/p/8414426.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值