题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542

题意:n个数中找m个数,使得从左到右读是上升的子序列。问一共有多少种。

dp(i,j)表示取到第i个位置,长为j并且最后一个数为a(i)的方案总数。

更新就比较容易了,dp(i,j)=∑(k=1->j-1)dp(i-1,k),初始化dp(i,1)=1。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define lowbit(x) x & (-x)
 4
 5 const int mod = int(1e9+7);
 6 const int maxn = 1010;
 7 int dp[maxn][maxn];
 8 int a[maxn], h[maxn];
 9 int n, m, hcnt;
10
11 int sum(int i, int x) {
12     int ret = 0;
13     while(x) {
14         ret = (ret + dp[i][x]) % mod;
15         x -= lowbit(x);
16     }
17     return ret;
18 }
19
20 void update(int i, int x, int k) {
21     while(x <= n) {
22         dp[i][x] = (dp[i][x] + k) % mod;
23         x += lowbit(x);
24     }
25 }
26
27 int getid(int x) {
28     return lower_bound(h, h+hcnt, x) - h;
29 }
30
31 int main() {
32     //freopen("in", "r", stdin);
33     int T, _ = 1;
34     scanf("%d", &T);
35     while(T--) {
36         printf("Case #%d: ", _++);
37         scanf("%d %d", &n, &m);
38         for(int i = 1; i <= n; i++) {
39             scanf("%d", &a[i]);
40             h[i-1] = a[i];
41         }
42         sort(h, h+n); hcnt = unique(h, h+n) - h;
43         for(int i = 1; i <= n; i++) a[i] = getid(a[i]) + 1;
44         memset(dp, 0, sizeof(dp));
45         for(int i = 1; i <= n; i++) {
46             for(int j = 1; j <= m; j++) {
47                 if(j == 1) {
48                     update(j, a[i], 1);
49                     continue;
50                 }
51                 int tmp = sum(j-1, a[i]-1);
52                 update(j, a[i], tmp);
53             }
54         }
55         printf("%d\n", sum(m, n));
56     }
57     return 0;
58 }

转载于:https://www.cnblogs.com/kirai/p/5955780.html

[HDOJ5542]The Battle of Chibi(DP,树状数组)相关推荐

  1. HDU-5542-The Battle of Chibi【树状数组+dp】

    HDU-5542-The Battle of Chibi[树状数组+dp] Time Limit: 6000/4000 MS (Java/Others) Memory Limit: 65535/655 ...

  2. HDU - 5542 The Battle of Chibi(树状数组+DP)

    UVA - 12983 The Battle of Chibi(树状数组+DP) HDU - 5542 The Battle of Chibi(树状数组+DP) #include<cstdio& ...

  3. HDOJ5542-The Battle of Chibi【树状数组,dp】

    正题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题目大意 求序列A有多少个长度为M的递增子序列. 解题思路 用fi,jfi,jf_{i,j ...

  4. 树形DP+树状数组 HDU 5877 Weak Pair

    1 //树形DP+树状数组 HDU 5877 Weak Pair 2 // 思路:用树状数组每次加k/a[i],每个节点ans+=Sum(a[i]) 表示每次加大于等于a[i]的值 3 // 这道题要 ...

  5. 小魂和他的数列(dp+树状数组优化)

    链接:https://ac.nowcoder.com/acm/contest/3566/C 来源:牛客网 Sometimes, even if you know how something's goi ...

  6. 牛客多校1 - Infinite Tree(虚树+换根dp+树状数组)

    题目链接:点击查看 题目大意:给出一个无穷个节点的树,对于每个大于 1 的点 i 来说,可以向点 i / minvid[ i ] 连边,这里的 mindiv[ x ] 表示的是 x 的最小质因数,现在 ...

  7. dp 树状数组 逆序元组

    wmq的队伍 发布时间: 2017年4月9日 17:06   最后更新: 2017年4月9日 17:07   时间限制: 2000ms   内存限制: 512M 描述 交大上课需要打卡,于是在上课前的 ...

  8. BZOJ.4553.[HEOI2016TJOI2016]序列(DP 树状数组套线段树/二维线段树(MLE) 动态开点)

    题目链接:BZOJ 洛谷 \(O(n^2)\)DP很好写,对于当前的i从之前满足条件的j中选一个最大值,\(dp[i]=d[j]+1\) for(int j=1; j<i; ++j)if(a[j ...

  9. HDU 2836 Traversal 简单DP + 树状数组

    题意:给你一个序列,问相邻两数高度差绝对值小于等于H的子序列有多少个. dp[i]表示以i为结尾的子序列有多少,易知状态转移方程为:dp[i] = sum( dp[j] ) + 1;( abs( he ...

  10. Codeforces 1096F(dp + 树状数组)

    题目链接 题意: 对于长度为$n$的排列,在已知一些位的前提下求逆序对的期望 思路: 将答案分为$3$部分 $1.$$-1$与$-1$之间对答案的贡献.由于逆序对考虑的是数字之间的大小关系,故假设$- ...

最新文章

  1. 知乎网解决HTML5 Placeholder的方案
  2. 最短路径问题经典题目汇总
  3. elementui 表格英文加数字排序_解决vue elementUI中table里数字、字母、中文混合排序问题...
  4. 在VB6.0中怎么实现escape和unescape
  5. OpenGL学习笔记_简介_环境配置_创建一个窗口实例
  6. 使用 Wowza 录制直播流 (HTTPLiveStreamRecord)
  7. python数据分析与挖掘实战---chapter8中医证型关联规则挖掘
  8. Salesforce入门教程(中文)-019 VF创建输入表单
  9. iOS-二级评论功能
  10. 高级UI-滤镜和颜色通道
  11. 画二元函数即三维图像的函数及matlab代码
  12. 关于Bundle Adjustment的直观理解
  13. 电脑PHP动画制作画板,涂鸦板简单实现 Html5编写属于自己的画画板
  14. 电源系统该如何选择电容大小及数量
  15. Jrebel最新激活破解方式(持续更新) 转
  16. 提质信创•协同发展—— 麒麟信安云+操作系统交流会(武汉站)顺利举行
  17. jeecg-boot实现分布式定时任务
  18. 优质的计算机软件著作权,计算机软件著作权登记后会公开吗?
  19. Unity优化材质,清除空引用贴图
  20. 让普通高校的学生被看见!

热门文章

  1. silverlight for olap version milestone 07 updated!
  2. 小技巧:用python迅速打印Java写 的Flink代码中的hive建表语句
  3. Java FlatMapFunction in Spark: error: is not abstract and does not override abstract method解决方案
  4. kaggle删除自己的数据集
  5. sublime,gedit,vim和mousepad等都出现fribidi_get_par_embedding_levels_ex
  6. OpenGL:纹理Textures
  7. php 频繁dom和 文件,性能优化之为什么不要频繁操作DOM
  8. 调用支付jsapl缺少参数:totalfee openid无法取得_微信支付的架构到底有多牛?
  9. [置顶]C#执行Excel宏模版的方法
  10. Django Rest框架 APIView源码调用