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. 配置组件的 props

    组件是相互独立.可复用的单元,一个组件可能在不同地方被用到.但是在不同的场景下对这个组件的需求可能会根据情况有所不同,例如一个点赞按钮组件,在我这里需要它显示的文本是“点赞”和“取消”,当别的同事拿过 ...

  2. 初学.net增删改查

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

  3. PKU_campus_2017_K Lying Island

    思路: 题目链接http://poj.openjudge.cn/practice/C17K/ 状压dp.dp[i][j]表示第i - k人到第i人的状态为j的情况下前i人中最多有多少好人. 实现: # ...

  4. ubuntu下安装方式汇总

    apt-get 可辅助通过 apt-cache search curl | grep php 查找已支持的插件,然后通过下面apt-get下载安装,例: apt-get install php5-cu ...

  5. UVA 11020 Efficient Solutions (BST,Splay树)

    题意:给n个坐标.一个坐标(x,y)若有无存在的坐标满足x1<x && y1<=y  或  x1<=x && y1<y 时,此坐标(x,y)是就 ...

  6. Gym - 100676G Training Camp (状压dp)

    G. Training Camp[ Color: Yellow ]Montaser is planning to train very hard for ACM JCPC 2015; he has p ...

  7. day24-2 单例模式

    目录 单例模式 类内部定义静态方法实现单例模式 装饰器实现单例模式 元类实现单例模式 单例模式 单例模式:基于某种方法实例化多次得到实例是同一个 当实例化多次得到的对象中存放的属性都一样的情况,应该将 ...

  8. wkhtmltopdf导出html到pdf

    1.使用背景     最近公司需要把html页面的内容生成pdf并下载,试过很多方法都没有满意的效果,后来找到wkhtmltopdf这款软件,终于解决了这个问题. wkhtmltopdf是exe文件, ...

  9. 部署 k8s Cluster(下)【转】

    上节我们通过 kubeadm 在 k8s-master 上部署了 Kubernetes,本节安装 Pod 网络并添加 k8s-node1 和 k8s-node2,完成集群部署. 安装 Pod 网络 要 ...

  10. PHP24 自定义分页类

    分页类的定义 <?php /** * Class MyPage 分页类 * @package core */ class MyPage { private $totalCount; //数据表中 ...