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 ...
随机推荐
- web中的编码问题
response返回有两种,一种是字节流outputstream,一种是字符流printwrite. 先说字节流,要输出“中国",给输出流的必须是转换为utf-8的“中国”,还要告诉浏览器, ...
- 多个网络请求成功返回再执行另外任务的思路分析(iOS)
前言 今天我们来讨论一个经常出现的需求场景,也是一个老话题.在开发中我们往往会遇到需要进行多个网络请求,并且需要多个网络请求成功返回后再做其他事的场景.比如同一个界面显示的内容需要用到两个网络接口,而 ...
- 结合canvas做雨滴特效
雨滴特效 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- IDEA中集成JRebel插件
下载下面2个插件 jr-ide-intellij-6.4.3_13-16.zip --- 官网的jar(地址:https://plugins.jetbrains.com/plugin/4441-jre ...
- shell脚本:批量修改文件名
参考链接1:shell脚本:批量修改文件名(删除文件名中字符) 参考链接2:linux shell 字符串操作详解 (长度,读取,替换,截取,连接,对比,删除,位置 ) 参考链接3:每天一个linux ...
- ZooKeeper-znode概念与使用
可以将ZK看作一个具有高可用性特征的文件系统.这个文件系统中没有文件和目录,而是统一使用节点(znode)的概念,称为znode.znode既可以作为保存数据的容器(如同文件),也可以作为保存其他zn ...
- 【转】php 操作数组(合并,拆分,追加,查找,删除等)
1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加.其形式为: array array_merg ...
- JAVA题库01
说出一些常用的类,包,接口,请各举5个 常用的类:BufferedReader BufferedWriter FileReader FileWirter String Integer java ...
- QT 使用QUdpSocket QUdpServer UDP 建立客户端与服务器端
1. 模拟天气监控,每隔两秒从Server发送天气信息到Client. 2. 示例代码 --------------------------- Server 端 ------------------- ...
- HDU4819 Mosaic
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...