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,然后今天下午也用树状数组搞了一下午,结果还是踩了和当时一样的坑:我总是把用来记录 ...
随机推荐
- kafka原理概念提炼
Kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性就是可以实 ...
- 怎样有效防止ddos
怎样有效防止ddos?当我们发现服务器被DDoS攻击的时候,不要过度惊慌失措,先查看一下网站服务器是不是被黑了,找出网站存在的黑链,然后做好网站的安全防御,开启IP禁PING,可以防止被扫描,关闭不需 ...
- tomcat nio apr
NIO[root@localhost ~]# vim /usr/local/tomcat9/conf/server.xml<Connector port="8080" pro ...
- PCA分析,及c++代码实现
本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/fengbingchun/article/details/79235028 主成分分析(Principal Co ...
- web storem破解
话不多说 直接跳地址 http://idea.lanyus.com/ 再加个附带汉化包,使用方法已经放在压缩包里面 网盘链接: https://pan.baidu.com/s/1aEA6SSbDuRg ...
- cin,cout优化
https://www.cnblogs.com/PrayG/p/5749832.html ios_base::sync_with_stdio(0); cin.tie(0); 涨知识了
- leetcood学习笔记-39-组合总和
题目描述: 方法一: class Solution: def combinationSum(self, candidates, target): """ :type ca ...
- SQL优化之慢查询和explain以及性能分析
性能优化的思路 首先需要使用慢查询功能,去获取所有查询时间比较长的SQL语句 使用explain去查看该sql的执行计划 使用show profile去查看该sql执行时的性能问题 MySQL性能优化 ...
- 归并排序c语言
void mergeAdd(int arr[], int left, int mid, int right, int *temp){ int i = left; ; int k = left;//临时 ...
- python 识别验证码自动登陆
# python 3.5.0 # 通过Chrom浏览器访问发起请求 # 需要对应版本的Chrom和chromdriver # 作者:linyouyi from selenium import webd ...