2274: [Usaco2011 Feb]Generic Cow Protests

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 196  Solved: 122
[Submit][Status]

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

HINT

Source

Gold

题解:

比较好的一道题。

刚开始看了没思路,后来想了想发现i+1 到 j 可以分一段等价与 s[i]>=s[j]

然后设 f[i]表示前 i 个数能分成的方案数,然后那么 f[i]=sigma(f[j]) s[i]>=s[j]  初值f[0]=1

然后我们发现这样暴力做的话是n^2的,那么考虑优化

既然 s[i]>=s[j] 时 f[j]才可以更新 f[i],那我们换一个思路来考虑,另 g[i] 表示前缀和第 i 大的前缀可以划分的方案总数

那么 f[i]=sigma(g[j])  1<=j<=s[i] 然后还要给 g[s[i]]+=f[i]

想到了什么?对了,就是树状数组。

离散化一下,然后注意一下边界就可以过了。

status没rank1表示很桑心

代码:

 #include<cstdio>

 #include<cstdlib>

 #include<cmath>

 #include<cstring>

 #include<algorithm>

 #include<iostream>

 #include<vector>

 #include<map>

 #include<set>

 #include<queue>

 #include<string>

 #define inf 1000000000

 #define maxn 150000

 #define maxm 500+100

 #define eps 1e-10

 #define ll long long

 #define pa pair<int,int>

 #define for0(i,n) for(int i=0;i<=(n);i++)

 #define for1(i,n) for(int i=1;i<=(n);i++)

 #define for2(i,x,y) for(int i=(x);i<=(y);i++)

 #define for3(i,x,y) for(int i=(x);i>=(y);i--)

 #define mod 1000000009

 using namespace std;

 inline int read()

 {

     int x=,f=;char ch=getchar();

     while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}

     while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}

     return x*f;

 }
int n,tot,s[maxn],a[maxn],c[maxn];
struct rec{int x,y;}b[maxn];
inline void change(int x,int y)
{
if(!y)return;
for(;x<=tot;x+=x&(-x))s[x]=(s[x]+y)%mod;
}
inline int sum(int x)
{
int t=;
for(;x;x-=x&(-x))t=(t+s[x])%mod;
return t;
}
inline bool cmp(rec a,rec b){return a.x<b.x;} int main() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); n=read();
for1(i,n)a[i]=a[i-]+read(),b[i].x=a[i],b[i].y=i;
sort(b,b+n+,cmp);
tot=;
for0(i,n)
{
if(i==||b[i].x!=b[i-].x)tot++;
c[b[i].y]=tot;
}
change(c[],);
for1(i,n-)change(c[i],sum(c[i]));
printf("%d\n",sum(c[n])); return ; }

BZOJ2274: [Usaco2011 Feb]Generic Cow Protests的更多相关文章

  1. 【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组

    题目描述 Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows ...

  2. [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. ...

  3. BZOJ 2274 [Usaco2011 Feb]Generic Cow Protests

    [题解] 很容易可以写出朴素DP方程f[i]=sigma f[j] (sum[i]>=sum[j],1<=j<=i).  于是我们用权值树状数组优化即可. #include<c ...

  4. USACO 奶牛抗议 Generic Cow Protests

    USACO 奶牛抗议 Generic Cow Protests Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望 ...

  5. 【BZOJ】【3301】【USACO2011 Feb】Cow Line

    康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...

  6. 洛谷 2344 奶牛抗议 Generic Cow Protests, 2011 Feb

    [题解] 我们可以轻松想到朴素的状态转移方程,但直接这样做是n^2的.所以我们考虑采用树状数组优化.写法跟求逆序对很相似,即对前缀和离散化之后开一个权值树状数组,每次f[i]+=query(sum[i ...

  7. [USACO11FEB]Generic Cow Protests

    思路: 动态规划.首先处理出这些数的前缀和$a$,$f_i$记录从第$1$位到第$i$位的最大分组数量.DP方程为:$f_i=max(f_i,f_j+1)$,其中$j$满足$a_i-a_j≥0$. # ...

  8. BZOJ3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 67  Solved: 39[Submit ...

  9. 3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 82  Solved: 49[Submit ...

随机推荐

  1. Failed dependencies: 检查依赖性错误 解决方法

    centOs下: error: Failed dependencies: 检查依赖性错误 解决方法 刚才安装avast的linux版,结果出现了: [root@localhost /]# rpm -i ...

  2. java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.L(转)

    09-09 10:19:59.979: E/AndroidRuntime(2767): FATAL EXCEPTION: main09-09 10:19:59.979: E/AndroidRuntim ...

  3. shell 求总分

    求总分并且输出: 文件a.txt 学号 姓名 性别 年龄 张三 男 赵四 男 李丽 女 文件b.txt 学号 语文 数学 英语 方法1: #!/bin/sh paste a.txt b.txt ccc ...

  4. Day6 - Python基础6 面向对象编程

    Python之路,Day6 - 面向对象学习   本节内容:   面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法.     引子 你现在是一家游戏公司的开发 ...

  5. 开发RESTful WebService

    RESTful风格的webservice越来越流行了,sun也推出了RESTful WebService的官方规范:JAX-RS,全称:Java API for RESTful WebService. ...

  6. Character Encoding tomcat.

    default character encoding of the request or response body: If a character encoding is not specified ...

  7. html5 高清屏幕图片处理

    1. srcset 语法:在元素上添加srcset属性.srcset的值是一个用逗号分隔的列表.列表中的每个项包含一张图片的路径并且按倍数(例如,1x,2x,3x...)提供多张分辨率的图片 参考:h ...

  8. SWT/RAP计算器

    /** *2.测试 */ public class NofTest extends AbstractEntryPoint {        Text text,text2;    RemoteObje ...

  9. JAVA-4-斐波列

    public class Ch049 { public static void main(String[] args) { // TODO 自动生成的方法存根 int a = 1, b = 1; fo ...

  10. java Enum 用法示例

    public enum MyEnum { Monday, Tuesday, Wednesday, Thursady, Friday, Saturday, Sunday; public static v ...