HDU5542 The Battle of Chibi
题意
给出长度为n的序列,问这个序列中有多少个长度为m的单调递增子序列。
\(1\le M\le N\le 1000\)
分析
用F[i,j]表示前j个数构成以Aj为结尾的数列中,长度为i的严格递增子序列有多少个
\]
复杂度\(O(mn^2)\)。
观察到决策集合的变化一是只增不减,二是有一个前缀范围,用树状数组维护转移即可。时间复杂度\(O(mn\log n)\)
代码
#include<iostream>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=1e3+1,INF=0x3f3f3f3f,mod=1e9+7;
int n,m,a[N],b[N],c[N],f[N][N],num;
void add(int x,int y){
for(;x<=n+1;x+=x&-x)
c[x]=(c[x]+y)%mod;
}
int ask(int x){
int ans=0;
for(;x;x-=x&-x)
ans=(ans+c[x])%mod;
return ans;
}
void The_Battle_of_Chibi(){
read(n),read(m);
for(int i=1;i<=n;++i) b[i]=read(a[i]);
sort(b+1,b+n+1);
for(int i=1;i<=n;++i) a[i]=lower_bound(b+1,b+n+1,a[i])-b+1;
a[0]=f[0][0]=1;
for(int i=1;i<=m;++i){
fill(c+1,c+n+2,0);
add(1,f[i-1][0]);
for(int j=1;j<=n;++j){
f[i][j]=ask(a[j]-1);
add(a[j],f[i-1][j]);
}
}
int ans=0;
for(int i=1;i<=n;++i) ans=(ans+f[m][i])%mod;
printf("Case #%d: %d\n",++num,ans);
}
int main(){
for(int t=read<int>();t--;) The_Battle_of_Chibi();
return 0;
}
HDU5542 The Battle of Chibi的更多相关文章
- hdu5542 The Battle of Chibi【树状数组】【离散化】
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- hdu5542 The Battle of Chibi[DP+BIT]
求给定序列中长度为M的上升子序列个数.$N,M<=1000$. 很容易想到方法.$f[i,j]$表示以第$i$个数结尾,长度为$j$的满足要求子序列个数.于是转移也就写出来了$f[i][j]+= ...
- 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)
题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...
- 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 ...
- 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 ...
- HDU - 5542 The Battle of Chibi(LIS+树状数组优化)
The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zho ...
- CDOJ 1217 The Battle of Chibi
The Battle of Chibi Time Limit: 6000/4000MS (Java/Others) Memory Limit: 65535/65535KB (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 ...
- 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 ...
随机推荐
- java笔记 -- java简单结构代码解析及注释
结构代码解析 public class FirstSample { public static void main(String[] args) { System.out.println(2.0-1. ...
- [python]windows截图
Windows截图 截图:截取屏幕任意大小图片 下载pip install Pillow from PIL import ImageGrab # x1:开始截图的x坐标 # x2:开始截图的y坐标; ...
- ONOS架构-概览
这个是阅读https://wiki.onosproject.org/display/ONOS/Architecture+Guide是顺便翻译的,目前断断续续在阅读,今天先贴一部分 概览 基于osgi, ...
- 使用pandas的部分问题汇总
pandas(我所用版本0.17)是一个强大数据处理库,在开发金融类系统中我应用到了pandas.Dataframe数据类型,它的数据结构类似一张图表(如下图所示),左边一列为index既行的索引: ...
- VSCode汉化
1.打开VSCode 点击箭头指示地方 在搜索框中输入chinese 然后安装中文简体 2.按住 Ctrl+shift+p 选择配置显示语言 然后会看见下面的样子 添加 "locale&q ...
- day048 BOM和DOM
BOM Browser Object Model(文档对象模型) > window对象 1.alert 弹出带有提示和一个确认按钮的警示框 2.confirm 弹出带有一 ...
- 【转】前端的BFC、IFC、GFC和FFC
什么是BFC.IFC.GFC和FFC CSS2.1中只有BFC和IFC, CSS3中才有GFC和FFC. FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念. ...
- 微信小程序textarea组件在fixed定位中随页面滚动
如果 textarea 是在一个 position:fixed 的区域,需要显示指定属性 fixed 为 true https://developers.weixin.qq.com/miniprogr ...
- python命名空间与作用域
python命名空间与作用域 命名空间是名称与对象之间的关系,可以将命名空间看做是字典,其中的键是名称,值是对象. 命名空间不共享名称. 在命名空间中的名称能将任何python对象作为值,在不同的 ...
- coursera-斯坦福-机器学习-吴恩达-笔记week2
1 多元线性回归 1.1 假设函数 多元线性回归是指有多个特征特征变量的情况.此时我们修改假设函数hθ(x)=θ0+θ1∗x为hθ(x)=θ0+θ1x1+θ2x2+⋯+θnxn.设x0=1,x为特征向 ...