hdu5542 The Battle of Chibi[DP+BIT]
求给定序列中长度为M的上升子序列个数。$N,M<=1000$。
很容易想到方法。$f[i,j]$表示以第$i$个数结尾,长度为$j$的满足要求子序列个数。于是转移也就写出来了$f[i][j]+=f[k][j-1]$ $(k<i且A_k<A_i)$。边界$f[0][0]=1$。
然后这是$O(N^2 M)$的。考虑优化。每次由于只从$j-1$也就是上一层状态中选在自己序号之前且比自己小的数来转移,用时间保证第一个要求,第二个要求,将每个数离散化一下,用$m$层的BIT维护以离散化值为下标的$f$值前缀和,每次查$j-1$这一层比自己离散化值小的,然后再把自己的dp值加入j这一层即可。$O(NMlogn)$。卡着过去了。
没什么要说的了。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define lowbit(x) (x&(-x))
#define dbg(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
typedef long long ll;
template<typename T>inline char MIN(T&A,T B){return A>B?A=B,:;}
template<typename T>inline char MAX(T&A,T B){return A<B?A=B,:;}
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=+,P=1e9+;
inline void inc(int&A,int B){A+=B;A>=P?A-=P:;}
int T,n,m,tot;
struct BIT{
int C[N];
inline void clear(){for(register int i=;i<=tot;++i)C[i]=;}
inline void Update_add(int x,int val){while(x<=tot)inc(C[x],val),x+=lowbit(x);}
inline int Query_sum(int x){int ret=;while(x>)inc(ret,C[x]),x-=lowbit(x);return ret;}
}sum[N];
int a[N],A[N],ans,tmp; int main(){//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);
read(T);for(register int o=;o<=T;++o){
read(n),read(m);A[tot=]=ans=;
for(register int i=;i<=n;++i)A[++tot]=read(a[i]);
sort(A+,A+tot+);tot=unique(A+,A+tot+)-A-;
for(register int i=;i<=m;++i)sum[i].clear();
sum[].Update_add(,);
for(register int i=;i<=n;++i){
int pos=lower_bound(A+,A+tot+,a[i])-A,tmp=;
for(register int j=_min(i,m);j;--j){
tmp=sum[j-].Query_sum(pos-);
sum[j].Update_add(pos,tmp);
if(j==m)inc(ans,tmp);
}
}
printf("Case #%d: %d\n",o,ans);
}
return ;
}
hdu5542 The Battle of Chibi[DP+BIT]的更多相关文章
- 2015南阳CCPC C - The Battle of Chibi DP
C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...
- hdu5542 The Battle of Chibi【树状数组】【离散化】
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- 2015南阳CCPC C - The Battle of Chibi DP树状数组优化
C - The Battle of Chibi Description Cao Cao made up a big army and was going to invade the whole Sou ...
- 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)
题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...
- ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...
- uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...
- HDU5542 The Battle of Chibi
题意 给出长度为n的序列,问这个序列中有多少个长度为m的单调递增子序列. \(1\le M\le N\le 1000\) 分析 用F[i,j]表示前j个数构成以Aj为结尾的数列中,长度为i的严格递增子 ...
- The Battle of Chibi(数据结构优化dp,树状数组)
The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...
- The 2015 China Collegiate Programming Contest C. The Battle of Chibi hdu 5542
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
随机推荐
- 添加code到github上
第一步:github上新建远程仓库 1. 在 https://github.com/ 注册账号 2. new 一个新仓库 (1) 点击加号下的`New repository` (2)在Reposit ...
- 一个可以查询CSS属性兼容性的网站。
平时遇到CSS属性是不是道理具体兼容哪些网站,就可以直接上这个网站查询啦.http://www.caniuse.com/ 这个是网站地址. 例如查询 inline-block属性兼容性 就可以看到 ...
- awk的输出格式控制:print 和printf
1.两个函数和若干个内部变量控制awk的输出格式: 两个函数:print和printf 内部变量:OFS:输出的列间隔符,默认为tab; ORS:输出的行间隔符,默认为\n printf更加自由化, ...
- 使用bedtools的一个问题
问题:有两个平行测序样本,分别得到1.vcf和2.vcf两个文件,想知道这两个文件有多少个重合点. [wangjq@mgmt CHG029194]$ cat t1 chr1 10 10 chr1 11 ...
- Python绿色版
Python 安装的时候,有个选项,是问你要安装给所有用户还是只安装给当前用户,你只要选择当前用户,就会把那些需要的 dll ,包括 msvcr90.dll 都给装到 Python 目录下,你只要把 ...
- Java Collection API
在 Java2中,有一套设计优良的接口和类组成了Java集合框架Collection,使程序员操作成批的数据或对象元素极为方便.这些接口和类有很多对抽象数据类型操作的API,而这是我们常用的且在数据结 ...
- QFile操作文件
1.构造QFile对象 QFile file("C:\a.txt"); 或者 QFile *file = new QFile("C:\a.txt"); 2.设置 ...
- Android LCD(一):LCD基本原理【转】
本文转载自:http://blog.csdn.net/longxiaowu/article/details/24787597 关键词:Android LCD TFT 液晶 偏光片 彩色滤光片 背光 ...
- java中的特殊有用类
1.MessageDigest:类似与md5加密算法应用的功能类
- js正则表达式验证(化繁为简)
以前用js写正则表达式验证,每一个文本框后面都要添加一个onblur函数,验证的信息少,也没体会到有多繁琐,这次项目中的页面比较多,页面中的信息也比较多,如果每个文本框都加一个验证函数的话,js验证代 ...