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. eclipse+axis2+webservice开发实例

    myeclipse10安装axis2插件 第一步:下载axis2-1.6的插件压缩包,axis2-eclipse-codegen-plugin-1.6.2.zip 和 axis2-eclipse-se ...

  2. 【AIX】AIX 6.1 “C compiler cc is not found”问题的解决方案

    一.问题的由来 前几天在AIX中安装部署 nginx-1.4.1,报如下错误: # cd nginx-1.4.1 # ./configure checking for OS  + AIX 1 0004 ...

  3. Redis的AOF功能

    引言:  Redis是基于内存的数据库,同时也提供了若干持久化的方案,允许用户把内存中的数据,写入本地文件系统,以备下次重启或者当机之后继续使用.本文将描述如何基于Redis来设置AOF功能 什么是R ...

  4. 没有懂的leetcode

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  5. Centos7+Apache2.4+php5.6+mysql5.5搭建Lamp环境——为了wordPress

    最近想搭建个人博客玩玩,挑来挑去发现口碑不错的博客程序是wordpress,简称wp.虽然是学java路线的程序员,但因入行时间太短,至今没有发现较为称手开源的博客程序,如果各位大神有好的推荐,也希望 ...

  6. 阿里UX矢量图标库–最强大的矢量图标库(Icon font制作力荐工具)

    继前面介绍过ICON-FONT的制作后,找了几个ICON库都是国外的今天偶然发现阿里巴巴的图标矢量库,www.iconfont.cn用了之后感觉很强大,丰富的图标库(集合阿里妈妈&淘宝的图标库 ...

  7. Dialog式的Activity(AndroidActivity生命周期)

    概述 和普通的Activity跳转稍微不同的是,当第1个Activity跳转到第二个Activity后,如果点击'back'按钮(即Android键盘的按钮,则不会调用调用第一个Activity的on ...

  8. centos6 x86 安装 oracle 11g2r 日志

    一.安装centos 6.5 二.安装ora所需的库 三.修改centos内核 四.建用户组和目录结构等 五.安装ora11g2r 六.安装sqlplus的翻页程序和help补丁 七.自启动脚本 八. ...

  9. iOS中使用正则表达式去掉HTML中的标签元素获得纯文本的方法

    content是根据网址获得的网页源码字符串 - (NSString *)changeToString:(NSString *)content { NSRegularExpression *regul ...

  10. codevs 1139 观光公交

    #include<cstdio> #include<cstdlib> #include<cstring> #define max(a,b) (a > b ? ...