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


        Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Problem Description
Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao’s army. But all generals and soldiers of Cao Cao were loyal, it’s impossible to convince any of them to betray Cao Cao.

So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.

Yu Zhou discussed with Gai Huang and worked out N information to be leaked, in happening order. Each of the information was estimated to has ai value in Cao Cao’s opinion.

Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact M information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the N information and just select M of them. Find out how many ways Gai Huang could do this.

Input
The first line of the input gives the number of test cases, T(1≤100). T test cases follow.

Each test case begins with two numbers N(1≤N≤103) and M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then N numbers in a line, the ith number ai(1≤ai≤109) indicates the value in Cao Cao’s opinion of the ith information in happening order.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the ways Gai Huang can select the information.

The result is too large, and you need to output the result mod by 1000000007(109+7).

Sample Input
2
3 2
1 2 3
3 2
3 2 1

Sample Output
Case #1: 3
Case #2: 0

Hint

In the first cases, Gai Huang need to leak 2 information out of 3. He could leak any 2 information as all the information value are in increasing order.
In the second cases, Gai Huang has no choice as selecting any 2 information is not in increasing order.

题目链接:HDU-5542

题目大意:给出长度为n的序列,问这个序列中有多少个长度为m的单调递增子序列。

题目思路:dp[i][j],表示到第i个数字,长度为j的单调递增子序列的个数。需要注意的是取第j个数字。

刚开始的想法:

        for (int i = 0; i < n; i++){dp[i][1] = 1;}for (int i = 1; i < n; i++){for (int j = 1; j <= min(i+1,m); j++){for (int k = i - 1; k >= 0; k--){if (num[k] < num[i])dp[i][j] = (dp[i][j] + dp[k][j - 1]) % MOD;}}}

n^3的复杂度会超时。利用树状数组优化第三层循环。

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
#define MOD 1000000007
#define  lowbit(x)  ((x) & -(x))
int num[1010];
int dp[1010][1010];
int b[1010];
int n,m;
int sum(int x,int y)   //计算y长度的子序列,前x项有多少种情况的和
{int ret = 0;while(x > 0){ret = (ret + dp[x][y]) % MOD;x -= lowbit(x);}return ret;
}
void add(int x,int y,int d)  //更新x及以上长度,能取到的y的值为d
{while(x <= n){dp[x][y] = (dp[x][y] + d) % MOD;x += lowbit(x);}
}
int main(){int t;scanf("%d",&t);int cnt = 1;while(t--){scanf("%d%d",&n,&m);memset(dp,0,sizeof(dp));for (int i = 1; i <= n; i++){scanf("%d",&num[i]);b[i] = num[i];}sort(b + 1,b + 1 + n);for (int i = 1; i<= n; i++){num[i] = lower_bound(b + 1, b + 1 + n, num[i]) - b;  //num[i]存储的是该位置是第几大的元素}for (int i = 1; i <= n; i++){for (int j = 1; j <= min(i+1,m); j++){if (j == 1) add(num[i],1,1);else{int temp = sum(num[i] - 1,j - 1);add(num[i],j,temp);}}}printf("Case #%d: %d\n",cnt++,sum(n,m));}return 0;
}

HDU-5542-The Battle of Chibi【树状数组+dp】相关推荐

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

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

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

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

  3. HDU 5792 World is Exploding(树状数组+离散化)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...

  4. hdu 4911 求逆序对数+树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...

  5. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  6. hdu 6447YJJ's Salesman 离散化+树状数组+DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 因为图中点的坐标值过大,达到1e9.然而只有1e5个点.所以先将其离散化.并按照<x.y& ...

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

    HDU 6447 YJJ's Salesman 题目 给一个二维数组,从(0,0)走到(1e9, 1e9).每次只能走右,下,右下,三个方向.其中只有通过右下走到特定给出的点(村庄)时才会获得分值.问 ...

  8. 【 HDU 1166】 敌兵布阵 树状数组从0到1

    如果给你一个数组,让你求某个区间的和,你很自然会想到遍历一遍数组,复杂度是O(n),但是如果有多次询问呢,你也许会想到用前缀数组,通过O(n)的预处理,达到O(1)的查询,但是如果要更新某个元素的值呢 ...

  9. HDU 5792 World is Exploding (树状数组)

    World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

最新文章

  1. 30 张图解 | 高频面试知识点总结:面试官问我高并发服务模型哪家强?
  2. python打地鼠游戏代码_打地鼠的游戏宝贝们都喜欢,快和宝贝一起动手画一幅《打地鼠》吧...
  3. leetcode 200岛屿的个数
  4. mask属性是css3的吗_CSS滤镜之Mask属性
  5. OC语法8——@class关键字
  6. pdf文档有时打开乱码的解决方案
  7. 三种交换方式:电路交换、分组交换、报文交换
  8. P3110 [USACO14DEC]驮运Piggy Back
  9. VMware vCenter/vSphere/vSan/Esxi/7.0 lic许可
  10. 在线微信公众号调查数据分析报告
  11. 解决OneNote复制 黏贴后是图片的问题?纯文本黏贴好用的免费软件
  12. 【心情分享】联系博主
  13. teamviewer13绿色便携版
  14. C# LINQ的Select与SelectMany函数
  15. 洛谷 P1914 小书童——密码
  16. USBKey数字证书导入操作系统
  17. Linux 电池 驱动
  18. Python+Selenium实现12306买票
  19. Foxmail邮箱使用方法
  20. 内置系统账户:Local system/Network service/Local Service 区别

热门文章

  1. c语言程序dx,求解含有三角函数的定积分c语言程序∫(1+cosπx)dx
  2. 职称计算机execl试题,职称计算机考试2017年Excel全真模拟试题
  3. 防盗号或炸骗不要输入qq密码怎么去除
  4. 区块链是什么,是否是一个骗局或者是不是有弊端??
  5. 打印店A4纸彩印多少钱一张?
  6. turtle_如何绘制菱形
  7. html5 css透明效果,css中实现背景透明的三种方式
  8. 跨平台的网络云盘软件设计实现【二】
  9. 虹科方案 | 工业树莓派作为软PLC的五种可能用途
  10. MSVCR140.dll文件丢失?程序无法运行?解决办法!!!