题目描述

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. Java设计模式(23)——行为模式之访问者模式(Visitor)

    一.概述 概念 作用于某个对象群中各个对象的操作.它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作. 引入 试想这样一个场景,在一个Collection中放入了一大堆的各种对象的引用 ...

  2. POM中常用依赖包

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  3. hdu1176免费馅饼(动态规划,数塔)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. 180706-BigDecimal除法的精度问题

    BigDecimal除法的精度问题 在使用BigDecimal的除法时,遇到一个鬼畜的问题,本以为的精度计算,结果使用返回0,当然最终发现还是自己的使用姿势不对导致的,因此记录一下,避免后面重蹈覆辙 ...

  5. Git与远程仓库关联以及关联错误解决方法

    假设你github的用户名是  helloworld  ,你在上面创建了一个 名为 hello 的 repository. 一. 与本地仓库进行关联 1.1用原生ssh进行关联,速度快: git re ...

  6. Spring Boot 示例项目

    Spring Boot 基于注解式开发 maven REST 示例项目    项目地址:https://github.com/windwant/spring-boot-service    项目地址: ...

  7. 逆波兰表达式[栈 C 语言 实现]

    逆波兰表达式 逆波兰表达式又叫做后缀表达式.在通常的表达式中,二元运算符总是置于与之相关的两个运算对象之间,这种表示法也称为中缀表示.波兰逻辑学家J.Lukasiewicz于1929年提出了另一种表示 ...

  8. 2018牛客多校第二场a题

    一个人可以走一步或者跳x步,但不能连着跳,问到这个区间里有几种走法 考虑两种状态  对于这一点,我可以走过来,前面是怎么样的我不用管,也可以跳过来但是,跳过来必须保证前一步是走的 dp[i][0]表示 ...

  9. python 文件编译成exe可执行文件。

    pyinstaller打包方法: pyinstaller安装参考地址:http://www.pyinstaller.org/ pywin32的下载地址:https://sourceforge.net/ ...

  10. [转]bashrc与profile区别

    作者:KornLee 2005-02-03 15:49:57 来自:Linux先生 /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/pro ...