【树状数组+dp】HDU 5542 The Battle of Chibi
http://acm.hdu.edu.cn/showproblem.php?pid=5542
【题意】
- 给定长为n的序列,问有多少个长为m的严格上升子序列?
【思路】
- dp[i][j]表示以a[i]结尾的长度为j的严格上升子序列有多少个
- dp[i][j]=sum{dp[k][j-1]},k小于i且a[k]<a[i]
- 区间dp,复杂度为O(n^3)
- 会超时,第三层循环求和用树状数组加速
- 时间复杂度为O(n^2logn)
- 离散化的时候排序后不去重(否则WA)
【Accepted】
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath> using namespace std;
typedef long long ll;
const ll mod=1e9+;
const int maxn=1e3+;
ll a[maxn];
ll b[maxn];
ll dp[maxn][maxn];
int cnt;
int n,m;
int lowbit(int x)
{
return x&(-x);
} ll add(int x,int y,ll d)
{
while(x<=n)
{
dp[x][y]=(dp[x][y]+d)%mod;
x+=lowbit(x);
}
} ll sum(int x,int y)
{
ll res=0ll;
while(x)
{
res=(res+dp[x][y])%mod;
x-=lowbit(x);
}
return res;
}
int main()
{
int T;
scanf("%d",&T);
int cas=;
while(T--)
{
memset(dp,,sizeof(dp));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
}
memcpy(b,a,sizeof(b));
sort(a+,a+n+);
//cnt=unique(a+1,a+n+1)-a-1;
for(int i=;i<=n;i++)
{
int pos=lower_bound(a+,a+n+,b[i])-a;
for(int j=;j<=min(i+,m);j++)
{
ll tmp;
if(j==) tmp=1ll;
else tmp=sum(pos-,j-);
add(pos,j,tmp);
}
}
ll ans=sum(n,m);
printf("Case #%d: %d\n",++cas,(int)ans);
}
return ;
}
【疑问】
为啥不可以去重?
【树状数组+dp】HDU 5542 The Battle of Chibi的更多相关文章
- 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 ...
- 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 4622 Reincarnation trie树+树状数组/dp
题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...
- HDU 6348 序列计数 (树状数组 + DP)
序列计数 Time Limit: 4500/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)
Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- hdu 2227(树状数组+dp)
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU 6447 YJJ’s Salesman (树状数组 + DP + 离散)
题意: 二维平面上N个点,从(0,0)出发到(1e9,1e9),每次只能往右,上,右上三个方向移动, 该N个点只有从它的左下方格点可达,此时可获得收益.求该过程最大收益. 分析:我们很容易就可以想到用 ...
- hdu 4991(树状数组+DP)
Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 南阳ccpc C题 The Battle of Chibi 树状数组+dp
题目: Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried ab ...
随机推荐
- 【HEVC简介】CTU、CU、PU、TU结构
参考文献:见<High Efficiency Video Coding (HEVC)>Block Structures and Parallelism Features in HEVC章 ...
- JSP和Servlet性能优化经验谈
你的J2EE应用是不是运行的很慢?它们能不能承受住不断上升的访问量?本文讲述了开发高性能.高弹性的JSP页面和Servlet的性能优化技术.其意思是建立尽可能快的并能适应数量增长的用户及其请求.在本文 ...
- 使用代码编辑器Sublime Text 3进行前端开发及相关快捷键
推荐理由: Sublime Text:一款具有代码高亮.语法提示.自动完成且反应快速的编辑器软件,不仅具有华丽的界面,还支持插件扩展机制,用她来写代码,绝对是一种享受.相比于浮肿沉重的Eclipse, ...
- dede网站目录权限设置
如果你的网站数据十分重要(那种两天就能弄好的垃圾站就算了),建议按本文所说的安全步骤进行严格的设置.1.目录权限 我们不建议用户把栏目目录设置在根目录, 原因是这样进行安全设置会十分的麻烦, 在默认的 ...
- javaee 第14周
1.web server Web Server中文名称叫网页服务器或web服务器.WEB服务器也称为WWW(WORLD WIDE WEB)服务器,主要功能是提供网上信息浏览服务.Web服务器可以解析( ...
- Two-Phase Commit (2PC)
两阶段提交模式像极了比赛发令:“预备,开始!”
- END - 提交当前的事务
SYNOPSIS END [ WORK | TRANSACTION ] DESCRIPTION 描述 END END 提交当前事务. 所有当前事务做的修改都可被其它事务看到并且保证在发生崩溃的情况下的 ...
- python基础一 day2
内容: 3%%s 输出:3%s 后面的全部转义 结果: 如果是因为执行break语句导致循环提前结束,就不会执行else. 单位换算: 编码方式: ascii unicode u ...
- CNN完成mnist数据集手写数字识别
# coding: utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data d ...
- 数位DP || Gym 101653R Ramp Number
每一位都大于等于前一位的数叫Ramp Number 给一个数,如果不是Ramp Number输出-1,如果是Ramp Number输出比它小的Ramp Number的个数 只和每一位上的数字有关 #i ...