C - The Battle of Chibi

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

  1. 2
  2. 3 2
  3. 1 2 3
  4. 3 2
  5. 3 2 1

Sample Output

  1. Case #1: 3
    Case #2: 0
  2.  
  3. 题意:给你n,m,n个数,让你找出长度为m的最长上升序列。
    题解:我们先按照一般思路想:dp[i][j]表示长度ja[i]结尾的上升子序列长度。
    显然这个复杂度是n^3的,我们可以用树状数组优化一层遍历变为n^2*logn在这之前先对a离散化。
    扫描一遍的同时,将a[j]的信息更新到树上,那么扫描就可以用 logn时间统计出k的信息.
  1.  
  1. ///
  2. #include<bits/stdc++.h>
  3. using namespace std ;
  4. typedef long long ll;
  5. #define mem(a) memset(a,0,sizeof(a))
  6. #define meminf(a) memset(a,127,sizeof(a));
  7. #define inf 1000000007
  8. #define mod 1000000007
  9. inline ll read()
  10. {
  11. ll x=,f=;char ch=getchar();
  12. while(ch<''||ch>''){
  13. if(ch=='-')f=-;ch=getchar();
  14. }
  15. while(ch>=''&&ch<=''){
  16. x=x*+ch-'';ch=getchar();
  17. }return x*f;
  18. }
  19. //************************************************
  20. const int maxn=+;
  21.  
  22. int sum[maxn][maxn],a[maxn],b[maxn],n,m;
  23. int getsum(int k,int x){
  24. int ans=;
  25. while(x>){
  26. ans=(ans+sum[k][x])%mod;
  27. x-=(x&-x);
  28. }
  29. return ans;
  30. }
  31. void update(int k,int x,int pos){
  32.  
  33. while(x<maxn){
  34. sum[k][x]=(sum[k][x]+pos)%mod;
  35. x+=(x&-x);
  36. }
  37. }
  38. int main(){
  39. int oo=;
  40. int T=read();
  41. while(T--){
  42. scanf("%d%d",&n,&m);
  43. for (int i = ; i <= n; i++){
  44. scanf("%d",&a[i]);
  45. b[i]=a[i];
  46. }
  47. mem(sum);
  48. sort(b + , b + + n);
  49. for(int i=;i<=n;i++)
  50. a[i]=lower_bound(b + , b + + n, a[i]) - b+;
  51. update(,,);
  52. int ans=,k;
  53. for(int i=;i<=n;i++){
  54. for(ans=,k=;k<m;k++){
  55. ans=getsum(k,a[i]-);
  56. if(ans==)break;
  57. update(k+,a[i],ans);
  58. }
  59. }
  60. printf("Case #%d: ",oo++);
  61. cout<<(getsum(m,n+)+mod)%mod<<endl;
  62. }
  63. return ;
  64. }

代码

  1.  

2015南阳CCPC C - The Battle of Chibi DP树状数组优化的更多相关文章

  1. 2015南阳CCPC C - The Battle of Chibi DP

    C - The Battle of Chibi Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Cao Cao made up a ...

  2. ccpc_南阳 C The Battle of chibi dp + 树状数组

    题意:给你一个n个数的序列,要求从中找出含m个数的严格递增子序列,求能找出多少种不同的方案 dp[i][j]表示以第i个数结尾,形成的严格递增子序列长度为j的方案数 那么最终的答案应该就是sigma( ...

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

  4. hdu5542 The Battle of Chibi【树状数组】【离散化】

    The Battle of Chibi Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Othe ...

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

  6. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

  7. 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路

    The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...

  8. 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)

    题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...

  9. 2015南阳CCPC D - Pick The Sticks 背包DP.

    D - Pick The Sticks Description The story happened long long ago. One day, Cao Cao made a special or ...

随机推荐

  1. Hive DDL&DML

    1.删除分区 ALTER TABLE table_name DROP IF EXISTS PARTITION(dt=') 如果是外部表,记得rm对应文件 2.添加分区 ALTER TABLE tabl ...

  2. ThinkPHP---layer插件

    [概论] (1)layer是基于jquery开发的一款美化弹框的插件,主要用于弹框效果的交互.但其他功能和组件也日益完善 官网:http://layer.layui.com 在线手册:http://w ...

  3. oracle中的冷热备份

    oracle有四种备份方法:冷备份.热备份.RMAN备份.逻辑备份. 其中冷备份和热备份都是用操作系统命令对oracle文件直接进行拷贝, 不同的是冷备份是把数据库关闭后再备份,备份过程中也要关闭数据 ...

  4. 简单的jsonp实现跨域原理

    什么原因使jsonp诞生?  传说,浏览器有一个很重要的安全限制,叫做"同源策略".同源是指,域名,协议,端口相同.举个例子,用一个浏览器分别打开了百度和谷歌页面,百度页面在执行脚 ...

  5. mysql数据库主从操作记录

    master数据库已投入生产一段时间后,做主从复制的操作记录 环境: master库:172.18.237.13slave库:172.18.237.14 mysql版本说明: master:mysql ...

  6. SQLAlchemy-Utils

    由于sqlalchemy中没有提供choice方法,所以借助SQLAlchemy-Utils组件提供的choice方法. 安装: pip3 install sqlalchemy_utils 示例: f ...

  7. Libreswan软件的密钥协商协议IKEv1主模式实现分析

    Libreswan软件的密钥协商协议IKEv1主模式实现分析 1 协商过程 IKEv1(互联网密钥交换协议第一版)是IPsec VPN隧道协商使用的标准密钥协商协议,其协商过程如下图所示. 总共来回交 ...

  8. 深入理解PHP之strpos

    概述 在php中经常用 strpos 判断字符串是否在另一个字符串中存在, 本文介绍 strpos 函数及其实现. strpos应用 <?php /* strpos示例 */ // test e ...

  9. git巧妙命令行

    git cherry-pick c7081607cfd1bfa99b6e6c70c208e71fbd8767ae

  10. nodejs fs.open

    fs.open(path, flags, [mode], [callback(err, fd)])是 POSIX open 函数的封装,与 C 语言标准库中的 fopen 函数类似.它接受两个必选参数 ...