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. [转]ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL

    本文转自:http://www.cnblogs.com/John-Connor/archive/2012/05/03/2478821.html 引言-- 在初级篇中,我们介绍了如何利用基于ASP.NE ...

  2. [转].NET MVC 分页以及增删查改

    本文转自:http://blog.csdn.net/sust2012/article/details/30761867 . 数据库操作,DAL 层: using System; using Syste ...

  3. vb6如何调用delphi DLL中的函数并返回字符串?

    1,问题描述 最近发现vb6调用delphi DLL中的函数并返回字符串时出现问题,有时正常,有时出现?号,有时干脆导致VB程序退出 -- :: 将金额数字转化为可读的语音文字:1转化为1元 ???? ...

  4. C# 移动控件

    最近要做车牌识别的,不同地区收费标准不一,所以想做个可以移动控件来给客户选择停车场收费条件的.   首先因为要自动排序控件选FlowLayoutPanel做容器,加若干Panel和FlowLayout ...

  5. 介绍三款大前端UI框架

    一.蚂蚁金服团队推出的基于React antd (全名:ant.design) 友情跳链:https://ant.design/index-cn:使用antd模板:https://pro.ant.de ...

  6. docker的网络配置

    Docker的4种网络模式 我们在使用docker run创建Docker容器时,可以用–net选项指定容器的网络模式,Docker有以下4种网络模式: host模式:使用–net=host指定. c ...

  7. poptip 外面 放 input 使用 iview vue

    外层套的是 <FormItem prop="name" label="姓名:"> <Input v-model="tFill.nam ...

  8. 关于sigleton模式

    单例模式的要点有三个:一是某个类只能有一个实例:二是它必须自行创建这个实例:三是它必须自行向整个系统提供这个实例. 从具体实现角度来说,就是以下三点:一是单例模式的类只提供私有的构造函数,二是类定义中 ...

  9. 关于在Qt里让程序休眠一段时间的方法总结

    出处:http://hanzhaoxin.cnblogs.com/ Qt 为何没有提供 Sleep 论坛上不时见到有人问: Qt 为什么没有提供跨平台的 sleep 函数? 使用平台相关的 Sleep ...

  10. linux 复制文件夹内所有文件到另一个文件夹

    cp -Rf /home/user1/* /root/temp/将 /home/user1目录下的所有东西拷到/root/temp/下而不拷贝user1目录本身.即格式为:cp -Rf 原路径/ 目的 ...