求给定序列中长度为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]的更多相关文章

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

  2. hdu5542 The Battle of Chibi【树状数组】【离散化】

    The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

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

  4. 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)

    题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...

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

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

  6. uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...

  7. HDU5542 The Battle of Chibi

    题意 给出长度为n的序列,问这个序列中有多少个长度为m的单调递增子序列. \(1\le M\le N\le 1000\) 分析 用F[i,j]表示前j个数构成以Aj为结尾的数列中,长度为i的严格递增子 ...

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

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

随机推荐

  1. yii2弹出层

    bootstrap http://getbootstrap.com/javascript/#modals https://github.com/lichunqiang/yii2-sweet-submi ...

  2. $Android连续按返回键两次实现退出程序

    思路:重写Activity的onKeyDown方法,判断按键是不是返回键,如果是,则再判断按下的时间和上次按下的时间之间的差值(毫秒数)是不是大于2000,如果不大于,则用finish()方法结束程序 ...

  3. VSCode eslint校验 tab改为2个空格

    修改:.eslintrc.json

  4. Linux环境下的图形系统和AMD R600显卡编程(2)——Framebuffer、DRM、EXA和Mesa简介

    转:https://www.cnblogs.com/shoemaker/p/linux_graphics02.html 1. Framebuffer Framebuffer驱动提供基本的显示,fram ...

  5. Linux基础四---系统监控&硬盘分区

    ---恢复内容开始--- 一系统分区 1.top [参数] -b 批处理 -c 显示命令完全模式 -I 忽略失效过程 -s 保密模式 -S 累积模式 -i<时间> 设置间隔时间 -u< ...

  6. Tooltip表单验证的注册表单

    在线演示 本地下载

  7. Redis 高级实用特性

    1.安全性 设置客户端连接后进行任何其他操作前先验证密码. 因为Redis速度相当快,所以在一台比较好的服务器下,一个外部用户可以在一秒钟进行150K次的密码尝试,这意味着需要指定一个非常强大的密码来 ...

  8. linux 安装mysql服务

    1.检查是否已安装,grep的-i选项表示匹配时忽略大小写 rpm -qa|grep -i mysql *可见已经安装了库文件,应该先卸载,不然会出现覆盖错误.注意卸:载时使用了--nodeps选项, ...

  9. 【bzoj3747】Kinoman[POI2015](线段树)

    题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3747 对于这种题,考虑固定区间的右端点为r,设区间左端点为l能取得的好看值总和为a[l] ...

  10. spring boot 基础学习

    构建微服务:Spring boot 入门篇 http://www.cnblogs.com/ityouknow/p/5662753.html SpringBoot入门系列:第一篇 Hello World ...