2015南阳CCPC C - The Battle of Chibi DP树状数组优化
C - The Battle of Chibi
Description
So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.
Yu Zhou discussed with Gai Huang and worked out N information to be leaked, in happening order. Each of the information was estimated to has ai value in Cao Cao's opinion.
Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact M information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the N information and just select M of them. Find out how many ways Gai Huang could do this.
Input
Each test case begins with two numbers N(1≤N≤103) and M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then N numbers in a line, the ith number ai(1≤ai≤109) indicates the value in Cao Cao's opinion of the ith information in happening order.
Output
The result is too large, and you need to output the result mod by 1000000007(109+7).
Sample Input
2
3 2
1 2 3
3 2
3 2 1
Sample Output
Case #1: 3
Case #2: 0 题意:给你n,m,n个数,让你找出长度为m的最长上升序列。
题解:我们先按照一般思路想:dp[i][j]表示长度j以a[i]结尾的上升子序列长度。
显然这个复杂度是n^3的,我们可以用树状数组优化一层遍历变为n^2*logn在这之前先对a离散化。
扫描一遍的同时,将a[j]的信息更新到树上,那么扫描就可以用 logn时间统计出k的信息.
///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define inf 1000000007
#define mod 1000000007
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//************************************************
const int maxn=+; int sum[maxn][maxn],a[maxn],b[maxn],n,m;
int getsum(int k,int x){
int ans=;
while(x>){
ans=(ans+sum[k][x])%mod;
x-=(x&-x);
}
return ans;
}
void update(int k,int x,int pos){ while(x<maxn){
sum[k][x]=(sum[k][x]+pos)%mod;
x+=(x&-x);
}
}
int main(){
int oo=;
int T=read();
while(T--){
scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
mem(sum);
sort(b + , b + + n);
for(int i=;i<=n;i++)
a[i]=lower_bound(b + , b + + n, a[i]) - b+;
update(,,);
int ans=,k;
for(int i=;i<=n;i++){
for(ans=,k=;k<m;k++){
ans=getsum(k,a[i]-);
if(ans==)break;
update(k+,a[i],ans);
}
}
printf("Case #%d: ",oo++);
cout<<(getsum(m,n+)+mod)%mod<<endl;
}
return ;
}
代码
2015南阳CCPC C - The Battle of Chibi DP树状数组优化的更多相关文章
- 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 ...
- ccpc_南阳 C The Battle of chibi dp + 树状数组
题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...
- HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 Problem DescriptionCao Cao made up a big army an ...
- 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 ...
- HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)
题目链接 2017 CCPC Harbin Problem K 题意 给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...
- 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路
The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...
- 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)
题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...
- 2015南阳CCPC D - Pick The Sticks 背包DP.
D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special or ...
随机推荐
- 平凡的世界小说txt下载完整版
<平凡的世界>正白纸黑字的告诉我们这样的人生真谛.它响亮的提出,人,无论在什么位置,无论多么贫寒,只要一颗火热的心在,只要能热爱生活,上帝对他就是平等的.只有作一名劳动者,不把不幸当作负担 ...
- Codeforces_766_D_(并查集)
D. Mahmoud and a Dictionary time limit per test 4 seconds memory limit per test 256 megabytes input ...
- std list/vector sort 自定义类的排序就是这么简单
所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index.但是要注意对象和对象指针的排序区别. 1.容器中是对象时,用操作符<或者比较函数,比较函数 ...
- CAD使用SetXData写数据(网页版)
主要用到函数说明: MxDrawEntity::SetXData 设置实体的扩展数据,详细说明如下: 参数 说明 [in] IMxDrawResbuf* pXData 扩展数据链表 js代码实现如下: ...
- centos7安装个人博客wordpress
第一步: 安装apache web 第二步: 安装MariaDB数据库 第三步: 安装PHP 第四步: 安装phpMyAdmin 第五部: 创建数据库: 第六部: 下载wordpress 第七部:复制 ...
- block:cfq 学习02
From: https://blog.csdn.net/vanbreaker/article/details/8308766 前文介绍了CFQ调度器的一些概念和结构之间的关系,这里再结合实际的代码,来 ...
- 话说Form标签的target属性-----无刷新表单提交
国庆前(2013)无聊,就在铁道部的12306上“逛”了下下. PS:原来之所以叫12306,是因为其客服号码是12306,好吧,我很无知…… 首先是被“逛”的页面:票价查询. 之所以去逛,是因为一直 ...
- 诊断:expdp导出时遇到错误ORA-31693和ORA-00922
11.2.0.1使用数据泵expdp导出时,如果使用parallel,可能会遇到 ORA-: Table data object "OWNER"."TABLE" ...
- mongodb--命令练习
windows10安装下载mongodb # 官网或镜像地址下载mongodb exe 二进制安装包 # 安装时选用custorm 去除勾选compass可视化工具 # 安装完成创建data文件,修改 ...
- centos 7桌面和命令行转行
CentOS7图形界面与命令行界面(终端)切换(1)CentOS7 在图形界面进入dos界面 :ctrl+alt+F6 dos界面进入图形界面:ctrl+alt+F2 本机用的这个命令: (2)Cen ...