求给定序列中长度为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. Loadrunder脚本篇——关联数组(参数数组)

    导言 前面说过可以用关联取出服务器相关的一些动态变化的信息,前面也提过web_reg_save_param中可以设置ord=all,代表从服务器中取出的是一个数组,它试用的场景是当我访问一个发帖网站, ...

  2. loadrunder之脚本篇——加密解密

    密码加密 可以给密码加密,意在把结果字符串作为脚本的参数或者参数值.例如,完整可能有一个用户密码填写的表单,你想测试网站针对不同密码的反应,但是你又想保护密码的安全.Password Encoder允 ...

  3. console.log()方法中%s的作用

    一.console.log("log信息"); 二.console.log("%s","first","second") ...

  4. 【iOS和HTML 5交互】iOS中加载html5调用html方法和修改html5内容

    近期项目开发中用到了这方面的技术了,那我们一起来看看. 1.利用webView控件加载本地html5或者网络上html5 2.设置控制器为webView的代理,遵守协议 3.实现代理方法webView ...

  5. JSP的动态Include的静态Include

    1. 静态导入示例 先总结: 1:静态include是把被引入的文件拼接到本页面中,再做为一个整体来编译,返回结果给客户端. 动态include是分别编译本页面和被引入的页面,再把结果合成一个html ...

  6. 考勤助手ER图2.0版本所存在的问题

    如图所示,考勤助手ER图2.0版本中,缺少个人信箱这一问题虽然不具有逻辑层面的问题,但是它是不满足需求的,也就是说这样的设计无法满足小组对考勤助手最初的设计.其次,就设计层面来分析,弱联系是数据库查询 ...

  7. Codeforces Round #412 div2 ABCD

    A 按rank给出每个人的赛前分数和赛后分数 如果一个人打败了比他分数高的人 他的分数必然升高 问比赛rated吗 如果一个人的分数改变了肯定rate 如果全都没改的话 也可能是rated 这时候ch ...

  8. Codeforces Round #373 (Div. 2) E. Sasha and Array 矩阵快速幂+线段树

    E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  9. keystone uwsgi failed

    ~$ /usr/local/bin/uwsgi /etc/keystone/keystone-uwsgi-public.ini[uWSGI] getting INI configuration fro ...

  10. shell基础之书写需要用到的小工具

    一.简单介绍 下面介绍几个可能不太常用但是面试题经常会用用到的小工具: 命令:cut 作用:截取某一个字段 '-d' 后面跟分隔字符,把字段分割为若干个区间. '-c' 后面接的是第几个字符,也可以是 ...