【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 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+树状数组的更多相关文章
- BZOJ2274: [Usaco2011 Feb]Generic Cow Protests
2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 196 Solve ...
- [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. ...
- BZOJ 2274 [Usaco2011 Feb]Generic Cow Protests
[题解] 很容易可以写出朴素DP方程f[i]=sigma f[j] (sum[i]>=sum[j],1<=j<=i). 于是我们用权值树状数组优化即可. #include<c ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- bzoj 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...
随机推荐
- mySql——case when else ....demo
DROP PROCEDURE IF EXISTS Pro_query_change_charge_by_layer_report; CREATE PROCEDURE Pro_query_change_ ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
- Android ObjectOutputStream Serializable引发的血案
遇到一个问题 安装后第二次进app,闪退 重现步骤 [前置条件] 打包分支:dev_7.13 手机:vivo NEX 8.1.0 [步骤] 安装三星app----同意用户协议进入书城---连续点击ba ...
- SpringBoot-07:SpringBoot整合PageHelper做多条件分页查询
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述如何在SpringBoot中整合PageHelper,如何实现带多个条件,以及PageInfo中的 ...
- IAR环境下编译CC2640入门开发
1. 安装SDK包,之后导入AIR里面,编译报错 看样子似乎是xdc工具的路径配置不对,进入路径配置对话窗 开始配置 配置完之后,重新编译 Fatal Error[Pe1696]: cannot op ...
- Python Map 并行
Map是一个酷酷的小东西,也是在Python代码轻松引入并行的关键.对此不熟悉的人会认为map是从函数式语言(如Lisp)借鉴来的东西.map是一个函数 - 将另一个函数映射到一个序列上.例如: ur ...
- git 取消commit
git如何撤销上一次commit操作 1.第一种情况:还没有push,只是在本地commit git reset --soft|--mixed|--hard <commit_id> git ...
- spark history server
参考:http://blog.csdn.net/lsshlsw/article/details/44786575 为什么需要historyServer? 在运行Spark Application的时候 ...
- Hadoop3.0新特性
1. Hadoop3.0简介 Hadoop 2.0是基于JDK 1.7开发的,而JDK 1.7在2015年4月已停止更新,这直接迫使Hadoop社区基于JDK1.8重新发布一个新的Hadoop版本,而 ...
- PS 去皱纹
1.打开一个有皱纹的图片,选择修复画笔工具,按住Alt键吸取一块光滑的皮肤,然后再在有皱纹的位置上点击即可