The Battle of Chibi

给出一段长度为n的序列\(\{a_i\}\),求其中长度为m的严格上升子序列个数\(mod\ 10^9+7\),\(n\leq 10^3\)。

不难想到设\(f[i][j]\)表示以第i个位置结尾,长度为j的LSIS,因此我们有

\[f[i][j]=\sum_{k=1,a_i>a_k}^{i-1}f[k][j-1]
\]

边界:\(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的更多相关文章

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

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

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

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

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

  5. CDOJ 1217 The Battle of Chibi

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

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

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

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

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

  9. hdu 5542 The Battle of Chibi(2015CCPC - C题)

    题目链接:hdu 5542 首届CCPC的C题,比赛时一起搞了好久,最后是队友A出的,当时有试过用树状数组来优化 dp,然后今天下午也用树状数组搞了一下午,结果还是踩了和当时一样的坑:我总是把用来记录 ...

随机推荐

  1. Hadoop 基础知识

    Hadoop 数据是存储在HDFS, Mapreduce 是一种计算框架,负责计算处理. HDFS上的数据存储默认是本地节点数据一份,同一机架不同节点一份,不同机架不同节点一份.默认是存储3份 HDF ...

  2. 8-vim-移动命令-03-段落切换和括号切换

    1.段落切换 vi使用空行来区分段落 在程序开发时,通常一段功能相关的代码会写在一起--之间没有空行 命令 功能 { 上一段 } 下一段 2.括号切换 在程序世界中,() [] {}使用频率很高,而且 ...

  3. 多flavor导致的源码依赖问题-- Cannot choose between the following configurations of project :sample:

    一.背景: 当我们在源码依赖的时候经常会导致一些问题. 我们的主工程有如下配置,它依赖了一个sample的本地工程 flavorDimensions "demo" productF ...

  4. 2018-2-13-win10-uwp-入门

    title author date CreateTime categories win10 uwp 入门 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23 ...

  5. Linux 进程间通信 消息队列 实现两个进程间通信

    例子: 通过消息队列实现两个进程间通信,一个进程从终端输入数据,通过消息队列发送,另一个进程通过消息队列接收数据 文件1 创建进程1 终端输入通过消息队列发送数据 #include <stdio ...

  6. 笔记52 Mybatis快速入门(三)

    一.更多查询 1.模糊查询 修改Category.xml,提供listCategoryByName查询语句select * from category where name like concat(' ...

  7. 使用sqlyog工具同步两个相同结构的数据库之间的数据

    compare two database data 因为工作上遇到 同一个项目被部署到不同服务器上,原项目(后统称"源")在运行中,后部署的项目(后统称"目标" ...

  8. Spring中使用到的设计模式

    1.工厂模式:Beanfactory和ApplicationContext 2.单例模式:bean的构建 3.代理模式:AOP 4.模板模式:jdbcTemplate,hibernateTemplat ...

  9. Android中的Parcel机制(上)

    一.先从Serialize说起 我们都知道JAVA中的Serialize机制,译成串行化.序列化--,其作用是能将数据对象存入字节流当中,在需要时重新生成对象.主要应用是利用外部存储设备保存对象状态, ...

  10. linux网络速率监控

    #!/bin/bash #作者:fafu_li #时间: #监控网卡传输速率 source /etc/profile #加载系统环境变量 source $HOME/.bash_profile #加载用 ...