【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( ...
随机推荐
- CSS基础part1
CSS 概述CSS 指层叠样式表 (Cascading Style Sheets),样式定义了如何显示 HTML文件中的标签元素,CSS是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标 ...
- 单目、双目和RGB-D视觉SLAM初始化比较
无论单目.双目还是RGB-D,首先是将从摄像头或者数据集中读入的图像封装成Frame类型对象: 首先都需要将彩色图像处理成灰度图像,继而将图片封装成帧. (1) 单目 mCurrentFrame = ...
- pyhon文件操作典型代码实现(非常经典!)
1. 编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图: 实现代码: import os all_files = os.listdir(os.chdir("D:\\" ...
- Unity Lighting - The Precompute Process 预计算过程(二)
The Precompute Process 预计算过程 In Unity, precomputed lighting is calculated in the background - eith ...
- Spring ApplicationContext 简介
ApplicationContext是对BeanFactory的扩展,实现BeanFactory的所有功能,并添加了事件传播,国际化,资源文件处理等. configure locations:(C ...
- TW实习日记:第31-32天
不知不觉的,实习的净工作天数,已经都超过一个月了.因为对工作内容不是很满意,所以打算月底离职,也不知道是公司太缺人还是我真的能干活,领导竟然三番两次找我让我再考虑...明天又要找我了,哎...随机应变 ...
- 【WXS数据类型】Date
生成 date 对象需要使用 getDate函数, 返回一个当前时间的对象. var date = getDate(); //返回当前时间对象 属性: 名称 值类型 说明 [Date].constru ...
- C二维数组行为空,列不为空
二维数组: 处理二维数组得函数有一处可能不太容易理解:数组的行可以在函数调用时传递,但是数组的列却只能被预置在函数内部. eg: #define COLS 4 int sum(int ar[][COL ...
- python读取日志,存入mysql
1.从 http://www.almhuette-raith.at/apache-log/access.log 下载 1万条日志记录,保存为一个文件,读取文件并解析日志,从日志中提取ip, time_ ...
- DeepLearning Intro - sigmoid and shallow NN
This is a series of Machine Learning summary note. I will combine the deep learning book with the de ...