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 ...
随机推荐
- Loadrunder脚本篇——webservice接口测试(二)
1.选择协议--Web Service,如下图 2.导入服务 入口1:点击Manage Services ->弹出窗中选择“Import” ->弹出窗中选择“URL”,填写wsdl地址,导 ...
- Linux查看某个端口+gcc动态编译
Linux下就: 1.lsof -i:端口号 2.netstat -tunlp|grep 端口号 gcc:动态编译 gcc –fpic –c file.c –o file.o gcc –shared ...
- HashTable的使用,扑克牌发牌游戏
l 场景 主要实现以下功能: 1. 首先给扑克牌中每张牌设定一个编号,下面算法实现的编号规则如下: 红桃按照从小到大依次为:1-13 方块按照从小到大依次为:14-26 黑桃按 ...
- Squid 访问控制配置
Squid 访问控制配置 主配置文件内加入限制参数 vim /etc/squid/squid.conf # 访问控制 acl http proto HTTP # 限制访问 good_domain添加两 ...
- java异常和错误类总结(2016.5)
看到以前2016.5.写的一点笔记,拿过来放在一起. java异常和错误类总结 最近由于考试和以前的面试经常会遇到java当中异常类的继承层次的问题,弄得非常头大,因为java的异常实在是有点多,很难 ...
- PhotoShopCs5启动 需要使用Adobe Application Manager 启动试用版
解决办法:下载Application Manager 7.0 地址:http://download.adobe.com/pub/adobe/creativesuite/cs/win/Applicati ...
- Centos6.5下Hbase安装
下载 http://mirror.bit.edu.cn/apache/hbase/hbase-0.94.26/hbase-0.94.26.tar.gz 2. 解压 tar -zxvf hbase-0 ...
- org.apache.flume.ChannelException: Take list for MemoryTransaction, capacity 100 full, consider committing more frequently, increasing capacity, or increasing thread count
flume在抽取MySQL数据到kafka时报错,如下 [SinkRunner-PollingRunner-DefaultSinkProcessor] ERROR org.apache.flume.s ...
- HDFS请求接口
通过HTTP来访问HDFS有两种方法:直接访问,HDFS后台进程直接服务于来自客户端的请求:通过代理(一对多)访问,客户端通常使用DistributedFileSystemAPI访问HDFS.第一种情 ...
- 批量测试邮箱登录python脚本
---恢复内容开始--- #!/usr/bin/env python #-*- coding:utf-8 -*- import smtplib import sys import time impor ...