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. 时间同步服务器NTP搭建

    NTP服务器        NTP(Network Time Protocol)[网络时间协议],它是用来同步网络中各个计算机的时间的协议,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒, ...

  2. 51-Ubuntu-打包压缩-1-打包压缩简介

    打包压缩是日常工作中备份文件的一种方式 在不同操作系统中,常用的打包压缩方式是不同的 Windows 常用 rar Mac 常用 zip Linux 常用 tar.gz

  3. Java split的用法

    java.lang.string.split split 方法 将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator,[limit]]) s ...

  4. splay模版

    //splay模版 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstrin ...

  5. 无法CREATE UNIQUE INDEX;找到重复的关键字

  6. 从零开始搭搭建系统3.1——顶级pom制定

    从零开始搭搭建系统3.1——顶级pom制定

  7. Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci...

    最近刚接触mysql,今天用mysql插入中文字符的时候,程序报错“Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_g ...

  8. java中的.java和.class的区别

    java文件就是这样一个未经编译的源程序,一般是给程序员看的. class文件就是被编译器编译过的java文件,通常是给计算机看的.

  9. 转化json里面的特殊字符

    前几天要做一个接受图片地址并将图片地址存放在数据库中,发现图片地址中有好多特殊字符反斜杠,中括号之类的,下面就是解决这个问题的代码 public String StringToJson(String ...

  10. Linux(二)高级文本处理

    一.cut (cut 命令可以从一个文本文件或者文本流中提取文本列 ) 1.cut语法 cut -d '分隔字符' -f fields         用于有特定分隔字符 cut  -c 字符区间   ...