题目描述

Farmer John's N (1 <= N <= 100,000) cows are lined up in a row andnumbered 1..N. The cows are conducting another one of their strangeprotests, 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 groupedand thus would like to arrange the cows into one or more contiguousgroups 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, and1, 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的余数。

输入

* Line 1: A single integer: N
* Lines 2..N + 1: Line i + 1 contains a single integer: A_i

输出

* Line 1: A single integer, the number of arrangements modulo 1,000,000,009.

样例输入

4
2
3
-3
1

样例输出

4


题解

dp+树状数组

设dp[i]为前i个数的划分方案数。

则易推出dp[i]=∑dp[j](sum[j]≤sum[i],j<i)。

那么可以用树状数组维护sum[j]在区间内的dp[j]的和。

由于sum过大且可能出现非正数,所以要先将sum离散化。

#include <cstdio>
#include <algorithm>
#define MOD 1000000009
using namespace std;
struct data
{
int sum , p;
}a[100010];
int f[100010] , dp[100010] , v[100010] , top;
bool cmp1(data a , data b)
{
return a.sum < b.sum;
}
bool cmp2(data a , data b)
{
return a.p < b.p;
}
void add(int p , int x)
{
int i;
for(i = p ; i <= top ; i += i & (-i))
f[i] = (f[i] + x) % MOD;
}
int query(int p)
{
int i , ans = 0;
for(i = p ; i ; i -= i & (-i))
ans = (ans + f[i]) % MOD;
return ans;
}
int main()
{
int n , i , t;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ )
scanf("%d" , &t) , a[i].sum = a[i - 1].sum + t , a[i].p = i;
sort(a , a + n + 1 , cmp1);
v[0] = 0x80000000;
for(i = 0 ; i <= n ; i ++ )
{
if(a[i].sum != v[top]) v[++top] = a[i].sum;
a[i].sum = top;
}
sort(a , a + n + 1 , cmp2);
dp[0] = 1;
add(a[0].sum , 1);
for(i = 1 ; i <= n ; i ++ )
dp[i] = query(a[i].sum) , add(a[i].sum , dp[i]);
printf("%d\n" , dp[n]);
return 0;
}

【bzoj2274】[Usaco2011 Feb]Generic Cow Protests dp+树状数组的更多相关文章

  1. BZOJ2274: [Usaco2011 Feb]Generic Cow Protests

    2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 196  Solve ...

  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. 树形DP+树状数组 HDU 5877 Weak Pair

    //树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...

  5. bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 793  Solved: 503[Submit][S ...

  6. 奶牛抗议 DP 树状数组

    奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...

  7. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  8. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  9. ccpc_南阳 C The Battle of chibi dp + 树状数组

    题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...

随机推荐

  1. NoSQL入门第五天——Java连接与整合操作

    一.测试联通 1.新建个web工程 2.导入jar:当然实际使用的时候肯定是通过maven来构建(如果有机会,可以尝试学习gradle进行构建) 3.建个测试类:好久没开eclipse了,希望后面可以 ...

  2. WPF程序,运行时,结束时,要运行的操作(自动保存,检查单程序)

    /// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { ...

  3. TCP/IP漫游

    TCP/IP漫游 TCP/IP是互联网的基础协议栈,它包括大大小小几十个协议.本篇文章主要涉及到就是HTTP.TCP.IP协议.我们经常学的网络模型是七层或者五层,实际上一般认为一共只有四层就可以了. ...

  4. eclipse插件SCON的SConscript文件和头文件以及C文件包含路径

    1. 本次的头文件路径\Hi2110-B657SP3-SDK\src_release_657SP3\src\lib\onenet\public,以此例子作为研究,本次开发使用eclipse,用到SCO ...

  5. JAVA基础学习之路(七)对象数组的定义及使用

    两种定义方式: 1.动态初始化: 定义并开辟数组:类名称 对象数组名[] = new 类名称[长度] 分布按成:类名称 对象数组名[] = null: 对象数组名 = new 类名称[长度]:   2 ...

  6. git revert 与 git reset

    Git版本回滚之 git revert 与 git reset 在使用 git 的时候,如果错误push之后,经常会回滚版本. git的回滚有两种方式: revert命令:这种方式,是用一种反向的 p ...

  7. 购物单:Excel的应用

    题目描述: 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞. 这不,XX大促销又来了!老板夫人开出了长长的购物单,都是有打折优惠 ...

  8. 基于Kubernetes(k8s)网络方案演进

    VIP PaaS在接近两年时间里,基于kubernetes主要经历四次网络方案的变迁: 1. kubernetes + flannel 2. 基于Docker libnetwork的网络定制 3. k ...

  9. 你真的了解JAVA里的String么

    Java中String类细节问题 (考察点Java内存分配问题) 1. String str1 = "abc";   System.out.println(str1 == &quo ...

  10. 针对“来用”团队项目之NABC分析

    本项目特点之一:扩展性强 NABC分析: N(need):我们这个开发的这个软件主要是集娱乐软件和实用工具于一身的大容器,这里面有很多应用程序,针对不同用户需要,至少有一款应用程序能够满足用户的需要, ...