【树状数组+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 ...
随机推荐
- 【前端】模拟微信上传图片(带预览,支持预览gif)
一.Html <style type="text/css"> #previewDiv{width:50px;height:50px;overflow:hidden;po ...
- 模板方法模式及php实现
模板方法模式: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.TemplateMethod 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 角色: 抽象模板角色:抽象模板类, ...
- 自定义Toast的显示位置和显示内容
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- 毕业设计:HomeFragment(二)
一.长按item时的响应 在长按item时,我希望能让CheckBox显示出来,并且呼出全选.反选.取消菜单,以及下载.删除.移动.复制操作菜单. 我在具体实现的时候发现处理item布局是一个很大的问 ...
- qt qtableview 样式设置
转载请注明出处:http://www.cnblogs.com/dachen408/p/7531159.html 1.设置tableview的列宽时,必须先setModel再setColumnWidge ...
- TensorFlow中屏蔽warning的方法
问题 使用sudo pip3 install tensorflow安装完CPU版tensorflow后,运行简单的测试程序,出现如下警告: I tensorflow/core/platform/cpu ...
- core 中使用 swagger
引包 代码 public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc().Set ...
- 沈南鹏@《遇见大咖》: A轮没投,投了8个月以后就证明了张一鸣是对了,在美国都没有张一鸣这种模式
沈南鹏@<遇见大咖>: A轮没投,投了8个月以后就证明了张一鸣是对了,在美国都没有张一鸣这种模式
- P1357 花园 (矩阵快速幂+ DP)
题意:一个只含字母C和P的环形串 求长度为n且每m个连续字符不含有超过k个C的方案数 m <= 5 n <= 1e15 题解:用一个m位二进制表示状态 转移很好想 但是这个题是用矩阵快速 ...
- java session cookie的使用
Cookie; Session; URL重写; cookie在J2EE项目中的使用,Java中把Cookie封装成了java.servlet.http.Cookie类.每个Cookie都是该Cooki ...