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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. hdu 4622 Reincarnation trie树+树状数组/dp

    题意:给你一个字符串和m个询问,问你l,r这个区间内出现过多少字串. 连接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 网上也有用后缀数组搞得. 思路 ...

  4. HDU 6348 序列计数 (树状数组 + DP)

    序列计数 Time Limit: 4500/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Subm ...

  5. hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

    Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  6. hdu 2227(树状数组+dp)

    Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/3 ...

  7. HDU 6447 YJJ’s Salesman (树状数组 + DP + 离散)

    题意: 二维平面上N个点,从(0,0)出发到(1e9,1e9),每次只能往右,上,右上三个方向移动, 该N个点只有从它的左下方格点可达,此时可获得收益.求该过程最大收益. 分析:我们很容易就可以想到用 ...

  8. hdu 4991(树状数组+DP)

    Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. 南阳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 ...

随机推荐

  1. android开发学习 ------- 【转】Genymotion 小白安装

    参考 https://www.cnblogs.com/whycxb/p/6850454.html 很详细,全面

  2. 初学.net增删改查

    分页显示 DAL: public List GetListByPager(int PageIndex, int PageSize, out int RowCount) { string sql = & ...

  3. 【转】深入理解Android中的SharedPreferences

    SharedPreferences作为Android中数据存储方式的一种,我们经常会用到,它适合用来保存那些少量的数据,特别是键值对数据,比如配置信息,登录信息等.不过要想做到正确使用SharedPr ...

  4. tcpdump 使用详解——转载

    http://www.cnblogs.com/ggjucheng/archive/2012/01/14/2322659.html 简介 用简单的话来定义tcpdump,就是:dump the traf ...

  5. 时间插件-daterangepicker

    一款基于bootstrap的时间插件daterangepicker,顾名思义,主要用于时间区间选择,也可做单个时间选择 demo.1汉化版的一个时间选择案例 <!DOCTYPE html> ...

  6. java语言基础-类型运算细节

    代码一: public class varDemo{ public static void main(String[] args) { byte a2; a2=3+4; System.out.prin ...

  7. js 跨域深入理解与解决方法

    参考博客: https://www.cnblogs.com/2050/p/3191744.html

  8. 微信小程序---目录结构

    (1)目录结构 小程序包含一个描述整体程序的 app 和多个描述各自页面的 page.一个小程序主体部分由三个文件组成,必须放在项目的根目录,如下: (2)允许上传的文件 .

  9. QT+创建两个不相干的窗口实现一个显示一个不显示

    因为两个窗口互不相干,所以需要重新创建一个窗口类subWidget subWidget.cpp文件 #ifndef SUBWIDGET_H #define SUBWIDGET_H #include & ...

  10. 使用jave2将音频wav转换成mp3格式

    最近需要用到语音合成功能,网上查阅了一番,发现可以使用腾讯云的语音合成API来完成这个功能,但是腾讯云的api返回的是wav格式的音频文件,这个格式的文件有些不通用,因此需要转换成mp3格式的文件.  ...