C - The Battle of Chibi
Description
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
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
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 ;
}
代码