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

2
3 2
1 2 3
3 2
3 2 1

Sample Output

Case #1: 3
Case #2: 0 题意:给你n,m,n个数,让你找出长度为m的最长上升序列。
题解:我们先按照一般思路想:dp[i][j]表示长度j以a[i]结尾的上升子序列长度。
显然这个复杂度是n^3的,我们可以用树状数组优化一层遍历变为n^2*logn在这之前先对a离散化。
扫描一遍的同时,将a[j]的信息更新到树上,那么扫描就可以用 logn时间统计出k的信息.

///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define inf 1000000007
#define mod 1000000007
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){
if(ch=='-')f=-;ch=getchar();
}
while(ch>=''&&ch<=''){
x=x*+ch-'';ch=getchar();
}return x*f;
}
//************************************************
const int maxn=+; int sum[maxn][maxn],a[maxn],b[maxn],n,m;
int getsum(int k,int x){
int ans=;
while(x>){
ans=(ans+sum[k][x])%mod;
x-=(x&-x);
}
return ans;
}
void update(int k,int x,int pos){ while(x<maxn){
sum[k][x]=(sum[k][x]+pos)%mod;
x+=(x&-x);
}
}
int main(){
int oo=;
int T=read();
while(T--){
scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
mem(sum);
sort(b + , b + + n);
for(int i=;i<=n;i++)
a[i]=lower_bound(b + , b + + n, a[i]) - b+;
update(,,);
int ans=,k;
for(int i=;i<=n;i++){
for(ans=,k=;k<m;k++){
ans=getsum(k,a[i]-);
if(ans==)break;
update(k+,a[i],ans);
}
}
printf("Case #%d: ",oo++);
cout<<(getsum(m,n+)+mod)%mod<<endl;
}
return ;
}

代码




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. C语言入门100题,考算法的居多

    入门题,考算法的居多,共同学习! 1. 编程,统计在所输入的50个实数中有多少个正数.多少个负数.多少个零. 2. 编程,计算并输出方程X2+Y2=1989的所有整数解. 3. 编程,输入一个10进制 ...

  2. HDU_2112_最短路

    题目链接:http://acm.hdu.edu.cn/status.php?user=l1526789512&pid=2112&status=5 HDU Today Time Limi ...

  3. IDEA中springboot项目打包成jar

      springboot的打包方式有很多种.有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的.这里主要介绍如何打成jar进行部署.不推荐用war,因为spri ...

  4. ls 命令还能这么玩?看一下这 20 个实用范例

    Linux中一个基本命令是ls.没有这个命令,我们会在浏览目录条目时会遇到困难.这个命令必须被每个学习Linux的人知道. ls是什么 ls命令用于列出文件和目录.默认上,他会列出当前目录的内容.带上 ...

  5. 数据类型对应字节数(32位,64位 int 占字节数)

    数据类型对应字节数(32位,64位 int 占字节数) 可用如sizeof(char),sizeof(char*)等得出 32位编译器: char :1个字节 char*(即指针变量): 4个字节(3 ...

  6. Nginx+Tomcat简单负载均衡

    Nginx,Apache安装完成 复制Tomcat:    tomcat-8080    tomcat-8081 启动Tomcat8080: cd /usr/local/tomcat-8080/bin ...

  7. 框架之---Flask

    Flask是一个轻量级的WEB框架,它和Django相比,就是一个胖子和一个骨架的区别. Flask是一个py文件就可以run的web框架,但是就因为是py文件就能run,所有,啥都没有,等把Flas ...

  8. react----父子组件之间的参数传递

    1.父组件向子组件传递参数 //父组件 import React from 'react'; import './header.css' import ComponentChild from './h ...

  9. react入门----基础语法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 洛谷月赛2018.8 T1题解(U28036 Nagisa loves Tomoya)

    [题解] 我们设原来的数组为a1,a2,a3..., 那么一次操作之后的数组变为a1+a2,a2+a3,a3+a4..., 两次操作之后数组变为a1+2a2+a3,a2+2a3+a4,a3+2a4+a ...