The Battle of Chibi
给出一段长度为n的序列\(\{a_i\}\),求其中长度为m的严格上升子序列个数\(mod\ 10^9+7\),\(n\leq 10^3\)。
解
不难想到设\(f[i][j]\)表示以第i个位置结尾,长度为j的LSIS,因此我们有
\]
边界:\(f[i][1]=1,i=1,2,...,n\),其余为0
答案:\(\sum_{i=1}^nf[i][m]\)
注意到这是\(O(n^3)\)算法,优先考虑转移优化,问题实际上是要寻找前面\(a_k\)小于\(a_i\)的f前缀和,考虑给a排序,也就是给a离散化,在离散化的数组里面建立前缀和数据结构(如线段树,树状数组),每次在对应的位置上查询修改前缀和,于是可以优化为\(O(n^2log^n)\)
参考代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define il inline
#define ri register
#define yyb 1000000007
using namespace std;
struct Map{
int a[1001],b[1001],n;
il void prepare(int m,int ar[]){
n=m;
for(ri int i(1);i<=n;++i)
a[i]=ar[i];sort(a+1,a+n+1);
for(ri int i(1);i<=n;++i)b[i]=dfs(ar[i]);
}
il int look(int x){
return b[x];
}
il int dfs(int x){
int l(1),mid,r(n);
while(l<=r){
mid=l+r>>1;
if(x>a[mid])l=mid+1;
else r=mid-1;
}return l;
}
}L;
struct lowbit{
int n,a[1001];
il void prepare(int m){
n=m,memset(a,0,sizeof(a));
}
il void change(int p,int v){
while(p<=n)(a[p]+=v)%=yyb,p+=-p&p;
}
il int ask(int p){
int ans(0);
while(p)(ans+=a[p])%=yyb,p-=-p&p;return ans;
}
}ar;
int a[1001],dp[1001][1001];
il void read(int&),work();
int main(){
int lsy,i;read(lsy);
for(i=1;i<=lsy;++i)printf("Case #%d: ",i),work();
return 0;
}
il void work(){
int n,m;read(n),read(m);
for(int i(1);i<=n;++i)read(a[i]);
L.prepare(n,a),memset(dp,0,sizeof(dp));
for(int i(1);i<=n;++i)dp[i][1]=1;
for(int i,j(2);j<=m;++j){
ar.prepare(n);
for(i=1;i<=n;++i)
dp[i][j]=ar.ask(L.look(i)-1),
ar.change(L.look(i),dp[i][j-1]);
}int ans(0);
for(int i(1);i<=n;++i)(ans+=dp[i][m])%=yyb;
printf("%d\n",ans);
}
il void read(int &x){
x&=0;ri char c;while(c=getchar(),c<'0'||c>'9');
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
}
The Battle of Chibi的更多相关文章
- 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 ...
- hdu5542 The Battle of Chibi【树状数组】【离散化】
The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Othe ...
- 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 ...
- ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...
- hdu 5542 The Battle of Chibi(2015CCPC - C题)
题目链接:hdu 5542 首届CCPC的C题,比赛时一起搞了好久,最后是队友A出的,当时有试过用树状数组来优化 dp,然后今天下午也用树状数组搞了一下午,结果还是踩了和当时一样的坑:我总是把用来记录 ...
随机推荐
- Feign 系列(04)Contract 源码解析
Feign 系列(04)Contract 源码解析 [TOC] Spring Cloud 系列目录(https://www.cnblogs.com/binarylei/p/11563952.html# ...
- HTTP信息头处理器
就是HTTP请求头-Header
- Linux特殊位SUID、SGID、SBIT
Linux特殊位SUID.SGID.SBIT 前言 Linux中的文件权限一般有x.w.r,在某个情况下有需要用到s.t,即特殊位. 进程运行时能够访问哪些资源或文件,不取决于进程文件的属主属组,而是 ...
- Pregel 消息传递机制
- Unity3d -- Collider(碰撞器与触发器)
(2d与3d的Collider可以相互存在,但是无法相互协作,如2d是无法检测3d的,反之,一样) 在目前掌握的情况分析,在Unity中参与碰撞的物体分2大块:1.发起碰撞的物体.2.接收碰撞的物体. ...
- tomcat之redis
Nginx服务器:[root@nginx ~]# vim /usr/local/nginx/conf/nginx.confupstream tomcat_pool { server 192.168.2 ...
- Vue.js 复选框
demo <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- JS自定义Array原型移除函数
<script type="text/javascript"> //删除元素值 Array.prototype.remove = function(element){ ...
- ThinkPHP框架数组定义
PHP数组定义 ThinkPHP框架中所有配置文件的定义格式均采用返回PHP数组的方式,格式为: //项目配置文件 return array( 'DEFAULT_MODULE' => 'Inde ...
- 「题解」:Kill
问题 A: Kill 时间限制: 1 Sec 内存限制: 256 MB 题面 题面谢绝公开. 题解 80%算法 赛时并没有想到正解,而是选择了另一种正确性较对的贪心验证. 对于每一个怪,我们定义它的 ...