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. caffe源代码分析--math_functions.cu代码研究

    当中用到一个宏定义CUDA_KERNEL_LOOP 在common.hpp中有. #defineCUDA_KERNEL_LOOP(i,n) \ for(inti = blockIdx.x * bloc ...

  2. TCP/IP协议族-----10、搬家IP

  3. 动画Rig设置为Legacy

    The animation state AimUp could not be played because it couldn't be found!Please attach an animatio ...

  4. 在 Java 中高效使用锁的技巧--转载

    竞争锁是造成多线程应用程序性能瓶颈的主要原因 区分竞争锁和非竞争锁对性能的影响非常重要.如果一个锁自始至终只被一个线程使用,那么 JVM 有能力优化它带来的绝大部分损耗.如果一个锁被多个线程使用过,但 ...

  5. Liunx Shell入门

    本人也是初学习Liunx,如有错误请指出.Liunx版本:Ubuntu 14.04 一.Liunx命令基础 在Ubuntu下打开终端快捷键为:ctrl+Alt+T Liunx命令的基本格式:comma ...

  6. nmap使用方法

      你是否曾想知道你所在局域网中哪些IP已经被使用了?还有哪些IP地址没有被使用?是否想知道某个IP地址下是什么系统…… 这些问题我们都可以使用一个nmap的工具解决,下面,就让我们开始了解nmap. ...

  7. iOS人脸识别(CoreImage)

    1.从初始UIImage获取一个CIImage对象. 2.创建一个用于分析对象的CIContext. 3.通过type和options参数创建一个CIDetector实例. type参数指定了要识别的 ...

  8. YYCache 源码分析(二)

    本文分析YYMemoryCache实现原理: YYMemoryCache是内存缓存,所以存取速度非常快,主要用到两种数据结构的LRU淘汰算法 1.LRU Cache的容量是有限的,当Cache的空间都 ...

  9. 从 ReactiveCocoa 中能学到什么?不用此库也能学以致用

    从知道ReactiveCocoa开始就发现对这个库有不同的声音,上次参加<T>技术沙龙时唐巧对在项目中已全面使用FRP的代码家提出为什么这种编程模型出现了这么长时间怎么像ReactiveC ...

  10. CoreText实现图文混排之点击事件

    今天呢,我们继续把CoreText图文混排的点击事件补充上,这样我们的图文混排也算是圆满了. 哦,上一篇的链接在这里 http://www.jianshu.com/p/6db3289fb05d Cor ...