[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;
}
[Usaco2011 Feb]Generic Cow Protests的更多相关文章
- BZOJ2274: [Usaco2011 Feb]Generic Cow Protests
2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 196 Solve ...
- 【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 ...
- BZOJ 2274 [Usaco2011 Feb]Generic Cow Protests
[题解] 很容易可以写出朴素DP方程f[i]=sigma f[j] (sum[i]>=sum[j],1<=j<=i). 于是我们用权值树状数组优化即可. #include<c ...
- USACO 奶牛抗议 Generic Cow Protests
USACO 奶牛抗议 Generic Cow Protests Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望 ...
- 【BZOJ】【3301】【USACO2011 Feb】Cow Line
康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...
- 洛谷 2344 奶牛抗议 Generic Cow Protests, 2011 Feb
[题解] 我们可以轻松想到朴素的状态转移方程,但直接这样做是n^2的.所以我们考虑采用树状数组优化.写法跟求逆序对很相似,即对前缀和离散化之后开一个权值树状数组,每次f[i]+=query(sum[i ...
- [USACO11FEB]Generic Cow Protests
思路: 动态规划.首先处理出这些数的前缀和$a$,$f_i$记录从第$1$位到第$i$位的最大分组数量.DP方程为:$f_i=max(f_i,f_j+1)$,其中$j$满足$a_i-a_j≥0$. # ...
- BZOJ3301: [USACO2011 Feb] Cow Line
3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 67 Solved: 39[Submit ...
- 3301: [USACO2011 Feb] Cow Line
3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 82 Solved: 49[Submit ...
随机推荐
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
- 【APUE】进程间通信之信号量
信号量是一个计数器,用于多进程对共享数据对象的访问 为了获得共享资源,进程需要执行下列操作: 1)测试控制该资源的信号量 2)若此信号量为正,则进程可以使用该资源,进程将信号量减1,表示它使用了一个资 ...
- CentOS 5 全功能服务器搭建
转自: http://www.php-oa.com/2007/12/27/centos-www.html 转:主要做为历史记录,以后用.另外很少见这么好的编译的文章,其实我不推荐用编译安装.但这个文章 ...
- Win7 丢失MSVCR110.DLL的解决办法
1 从下面的网站下载dll文件 http://www.ddooo.com/softdown/27034.htm 2 把该文件放到C:\Windows\SysWOW64目录下(64位系统)或者C:\ ...
- 【转载】TCP,IP,HTTP,SOCKET区别和联系
网络由下往上分为: 对应 物理层-- 数据链路层-- 网络层-- IP协议 传输层-- TCP协议 ...
- STM32 USB复合设备编写
目的 完成一个CDC + MSC的复合USB设备 可以方便在CDC,MSC,复合设备三者间切换 可移植性强 预备知识 cube中USB只有两个入口. main函数中的MX_USB_DEVICE_Ini ...
- 云上领跑,快人一步:华为云抢先发布Redis5.0
12月17日,华为云在DCS2.0的基础上,快人一步,抢先推出了新的Redis 5.0产品,这是一个崭新的突破.目前国内在缓存领域的发展普遍停留在Redis4.0阶段,华为云率先发布了Redis5.0 ...
- 电子设计省赛--PID
//2014年4月17日 //2014年6月20日入"未完毕"(未完毕) //2014年6月21日 一開始还以为是多难的算法.事实上就是个渣渣. 当然PID实践中应该会非常难. 另 ...
- mongodb06---索引
索引(就像书的目录,先找大致的位置再细找,不是从头开始找): mongodb每行的列可以完全不同,没有列的概念. 索引作用类型: :单列索引 :多列索引 :子文档索引 索引性质: :普通索引 .唯一索 ...
- RK3288-OTA编译失败解决办法【转】
本文转载自:http://blog.csdn.net/wangxueming/article/details/52448630 在执行make otapackage的时候出现该错误,是由于drmsi ...