奶牛抗议 DP 树状数组

USACO的题太猛了

容易想到\(DP\),设\(f[i]\)表示为在第\(i\)位时方案数,转移方程:

\[f[i]=\sum f[j]\;(j< i,sum[i]-sum[j]\ge0)
\]

\(O(n^2)\)过不了,考虑优化

移项得:

\[f[i]=\sum f[j]\;(j< i,sum[i]\ge sum[j])
\]

这时候我们发现相当于求在\(i\)前面并且前缀和小于\(sum[i]\)的所有和,这就可以用一个树状数组优化了,在树状数组维护下标为\(sum[i]\),\(f[i]\)的前缀和。对于每个\(f[i]\)即为树状数组上\(sum[i]\)的前缀和。

这里需要注意的是前缀和可能为负,而树状数组下标不能为负,所以我们要离散化一下。

#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 100010
#define lowbit(x) ((x)&(-(x)))
#define MOD 1000000009
int n,sum[MAXN],s;
int sum_sort[MAXN+1];
int tre[MAXN+1];
inline void add(int x, int val){
while(x<=s){
tre[x]=(tre[x]+val)%MOD;
x+=lowbit(x);
}
}
inline int get_sum(int x){
int res=0;
while(x>0){
res=(res+tre[x])%MOD;
x-=lowbit(x);
}
return res;
}
int main(){
scanf("%d", &n);
for(int i=1;i<=n;++i)
scanf("%d", &sum[i]),sum[i]+=sum[i-1];
for(int i=1;i<=n;++i) sum_sort[i]=sum[i];
sort(sum_sort, sum_sort+1+n);
s=unique(sum_sort, sum_sort+1+n)-sum_sort;
for(int i=0;i<=n;++i) sum[i]=lower_bound(sum_sort, sum_sort+s, sum[i])-sum_sort+1;
add(sum[0], 1); // f[0]=1 计数dp初始化
int ans=0;
for(int i=1;i<=n;++i){
ans=get_sum(sum[i]); // 获得f[i]
add(sum[i], ans); // 维护树状数组
}
printf("%d\n", ans);
return 0;
}

奶牛抗议 DP 树状数组的更多相关文章

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

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

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

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

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

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

  5. [USACO]奶牛抗议(DP+树状数组+离散化)

    Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望奶牛在抗议时保持理性,为此,他打算将所有的奶牛隔离成 若干个小组 ...

  6. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

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

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

  8. ccpc_南阳 C The Battle of chibi dp + 树状数组

    题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...

  9. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

随机推荐

  1. Elasticsearch7.3使用内置的JDK12

    汇总:采用最简单的办法,就是在elasticsearch文件开头添加上这一行export JAVA_HOME=/home/vdb1/elastic_cluster/elasticsearch-7.3. ...

  2. 《JAVA高并发编程详解》-volatile和synchronized

  3. win10 mars xlog编译

    win10 mars xlog编译   一. 环境准备 安装 cmake 以及 python2.7, 以及下载 ndk-r16b,并配置环境变量 NDK_ROOT 指向 ndk 路径. 如果是 Win ...

  4. Golang slice和map的申明和初始化

    1 前言 仅供记录使用. 2 代码 /** * @Author: FB * @Description: * @File: SliceMapInit.go * @Version: 1.0.0 * @Da ...

  5. 记录screen屏幕日志

    1.建立日志存放目录#mkdir /var/log/screen/ 2.修改配置文件,在末尾添加配置内容#vi /etc/screenrclogfile /var/log/screen/%t.log ...

  6. shell截取字符串操作

    举例变量:url=http://www.baidu.com/123456.html 1. # 号截取,删除左边字符,保留右边字符. echo ${url#*//} # 其中 url 是变量名,# 号是 ...

  7. 【转载】使用宝塔对Linux系统进行界面化管理操作

    腾讯云服务器和阿里云服务器的Centos系统都是没有Linux系统的一个版本,Centos系统的操作都是在没有类似Windows图形化操作界面的黑框框命令窗口进行操作的,需要使用到很多Linux操作命 ...

  8. 编辑/etc/passwd文件进行权限升级的技巧

    0x00 前言 在本文中,我们将学习“修改/etc/passwd文件以创建或更改用户的root权限的各种方法”.有时,一旦目标被攻击,就必须知道如何在/etc/passwd文件中编辑自己的用户以进行权 ...

  9. scrapy随机切换user-agent

    使用github的 scrapy-fake-useragent 不用自己改源码继承自带的userAgent中间件  只需要安装后增加配置即可 https://github.com/alecxe/scr ...

  10. div折角~~~

    代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...