[USACO]奶牛抗议(DP+树状数组+离散化)
Description
约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动。第i头奶牛的理智度 为Ai,Ai可能是负数。约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组,每个小组内的奶牛的理智度总和都要大于零。由于奶牛是按直线排列的,所以 一个小组内的奶牛位置必须是连续的。
请帮助约翰计算一下,存在多少种不同的分组的方案。由于答案可能很大,只要输出答 案除以1,000,000,009的余数即可。
Solution
容易想到设\(F[i]\)表示 到第头\(i\)奶牛的方案数,
那么就有\(F[i]=\sum F[j](sum[i]-sum[j] \geq 0)\), sum为前缀和,
但如果直接枚举时间大概为\(O(n^2)\),显然会超时,那么考虑优化
式子可化为\(F[i]=\sum F[j](sum[i]\geq sum[j])\) ,
可以用前缀和为下标,构造树状数组维护小于等于\(sum[i]\)的\(F\) 的和
那么前缀和很大,但是中间有很多没用的,离散化即可
注意\(F[0]=1\)要提前预处理进树状数组,时间复杂度\(O(nlogn)\)
Code
#include <cstdio>
#include <algorithm>
#define LL long long
#define N 1000010
using namespace std;
struct info {
int id;
LL sum;
} a[N];
const int yh = 1e9 + 9;
int n, t, p[N];
LL Ans, f[N];
int lowbit(int x) {
return x & (-x);
}
inline int read() {
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-')f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
bool cmp(info a, info b) {return a.sum < b.sum;}
void add(int x, LL v) {
while (x <= n) {
f[x] = (f[x] + v) % yh;
x += lowbit(x);
}
}
LL cal(int x) {
LL r = 0;
while (x) {
r = (r + f[x]) % yh;
x -= lowbit(x);
}
return r;
}
int main() {
n = read();
for (int i = 1; i <= n; ++i) {
a[i].sum = a[i - 1].sum + read();
a[i].id = i;
}
a[n + 1].id = n + 1; //F[0]=1
a[n + 1].sum = 0;
sort(a + 1, a + n + 2, cmp);
for (int i = 1; i <= n + 1; i++) { //离散化
if (i == 1 || a[i].sum != a[i - 1].sum) ++t;
p[a[i].id] = t;
}
add(p[n + 1], 1);
for (int i = 1; i <= n; ++i) {
Ans = cal(p[i]);
add(p[i], Ans);
}
printf("%lld\n", Ans);
return 0;
}
[USACO]奶牛抗议(DP+树状数组+离散化)的更多相关文章
- 奶牛抗议 DP 树状数组
奶牛抗议 DP 树状数组 USACO的题太猛了 容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程: \[ f[i]=\sum f[j]\;(j< i,sum[i] ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- bzoj 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛【dp+树状数组+hash】
最长上升子序列.虽然数据可以直接n方但是另写了个nlogn的 转移:f[i]=max(f[j]+1)(a[j]<a[i]) O(n^2) #include<iostream> #in ...
- JZYZOJ 1360 [usaco2011feb]人品问题 DP 树状数组 离散化
http://172.20.6.3/Problem_Show.asp?id=1360 好想好写 代码 #include<iostream> #include<cstdio&g ...
- 树形DP+树状数组 HDU 5877 Weak Pair
//树形DP+树状数组 HDU 5877 Weak Pair // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 // 这道题要离散化 #i ...
- 【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 1264 [AHOI2006]基因匹配Match(DP+树状数组)
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 793 Solved: 503[Submit][S ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
随机推荐
- C++中的swap函数
最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符 template <class T> void swap ( T& a, T& b ) { T c(a); a ...
- ashx是什么文件
ashx是什么文件 .ashx 文件用于写web handler的..ashx文件与.aspx文件类似,可以通过它来调用HttpHandler类,它免去了普通.aspx页面的控件解析以及页面处理的过程 ...
- log4net 最快速体验
本文供实习司机快速上手log4net最基本功能,共4步,3分钟搞定. 一.添加log4net.dll引用,可使用nuget安装或直接引用文件 二.添加配置 在app.config或web.config ...
- webpack.config.js====引入Jquery库文件
1. 安装 cnpm install --save jquery expose-loader 2. 在webpack.config.js中配置 Jquery库是使用的webpack的一个插件Provi ...
- 使用AOP监控用户操作并插入数据库
引入依赖 <!--spring切面aop依赖--> <dependency> <groupId>org.springframework.boot</group ...
- subline 安装 package control 连接服务器失败,解决办法
解决办法: 打开C:\Windows\system32\drivers\etc\hosts文件 增加 50.116.34.243 sublime.wbond.net50.116.34.243 pack ...
- A promise tomorrow is worth a lot less than trying today.
A promise tomorrow is worth a lot less than trying today.明日的承诺远不及今日的行动.
- [转]Git之忽略文件(ignore file)
原文链接:http://blog.csdn.net/benkaoya/article/details/7932370 .gitignore 配置文件用于配置不需要加入版本管理的文件,配置好该文件可以为 ...
- IOS防作弊产品技术原理分析
由于时间和水平有限,本文会存在诸多不足,希望得到您的及时反馈与指正,多谢! 工具环境: iPhone 6.系统版本 10.1.1IDA Pro 7.0 0x00:防作弊产品介绍 1.由于IOS系统的不 ...
- DataGridView控件使用大全
转自:http://www.cnblogs.com/xiaofengfeng/archive/2011/04/16/2018504.html DataGridView控件 DataGridView是用 ...