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+树状数组+离散化)的更多相关文章

  1. 奶牛抗议 DP 树状数组

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

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

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

  3. 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 ...

  4. JZYZOJ 1360 [usaco2011feb]人品问题 DP 树状数组 离散化

    http://172.20.6.3/Problem_Show.asp?id=1360   好想好写   代码 #include<iostream> #include<cstdio&g ...

  5. 树形DP+树状数组 HDU 5877 Weak Pair

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

  6. 【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 ...

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

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

  8. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

随机推荐

  1. SpringBoot | 第十五章:基于Postman的RESTful接口测试

    前言 从上一章节开始,接下来的几个章节会讲解一些开发过程中配套工具的使用.俗话说的好,工欲善其事,必先利其器.对于开发人员而言,有个好用的工具,也是一件事半功倍的事,而且开发起来也很爽,效率也会提升很 ...

  2. java如何调用服务端的WSDL接口

    如何使用http://192.168.0.170:8090/kaoshi?wsdl调用服务端暴露在外面可以使用的接口 1.首先创建调用ws的web项目,就一个普通的web项目就行: 2.通过eclip ...

  3. c#异步Begininvoke使用随笔

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 2019年我的nodejs项目选型

    选型项目比较激进.发现基于 go 语言的工具变多了.

  5. 谷歌地图自定义popup框

    谷歌地图的infowindow 不提供官方的定制化 <!DOCTYPE html> <html> <head> <meta name="viewpo ...

  6. BZOJ3798: 特殊的质数(分段打表)

    题意 题目链接 Sol 分块打表,非常好的思想. 对于这种求$[A, B]$区间内xxx的数的个数,然后$B$又不算是特别大的题,考虑分段打表 每个块的大小为$10^5$,打$3 * 10^3$个.然 ...

  7. Angular ui-route介绍

    参考博客: https://www.cnblogs.com/haogj/p/4885928.html 原文地址:http://www.ng-newsletter.com/posts/angular-u ...

  8. Java Web中web.xml文件简单介绍

    参考博客: https://www.cnblogs.com/Y-oung/p/8401549.html 1.XML 声明和根元素 <?xml version="1.0" en ...

  9. RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍

    1. AMF AMF(是Action Message Format的缩写)是在flash和flex中与远程服务端交换数据的一种格式.它是二进制格式,Flash应用与服务端或数据库通过RPC交换数据时, ...

  10. [转贴] 2016一月12日起.NET 4, 4.5 and 4.5.1 停止安全更新、技术支持 or hotfix

    [转贴] 2016一月12日起.NET 4, 4.5 and 4.5.1 停止安全更新.技术支持 or hotfix https://www.dotblogs.com.tw/mis2000lab/20 ...