C - The Battle of Chibi
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
无
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
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
HINT
题意
给你n个数,求长度为m的上升子序列有多少个
题解:
n^3方法:dp[i][j]表示结尾为i,长度为j的有多少个,转移是O(n)的、
那么我们用树状数组优化转移过程,优化到n^2logn就可以AC了
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 1000000005
#define eps 1e-12
#define PI acos(-1.0)
#define LL long long using namespace std; const int maxn = ;
const int mod = ;
inline long long read()
{
long long x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int a[maxn], b[maxn], c[maxn][maxn], n, m, dp[maxn][maxn]; inline void Init()
{
for (int i = ; i <= n; i++)
{
a[i] = b[i] = ;
for (int j = ; j <= n; j++)
dp[i][j] = c[i][j] = ;
}
return;
} inline int Lowbit(int x)
{
return x & (- x);
} inline void Update(int l, int pos, int key)
{
while(pos <= n)
{
c[l][pos] = (c[l][pos] + key);
while(c[l][pos]>=mod)
c[l][pos] -= mod;
pos = pos + Lowbit(pos);
}
return;
} inline int Sum(int l, int pos)
{
int temp = ;
while(pos)
{
temp = (temp + c[l][pos]);
while(temp >= mod)temp -= mod;
pos = pos - Lowbit(pos);
}
return temp;
} int main()
{
int T, cas = ;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
Init();
for (int i = ; i <= n; i++)
{
a[i]=read();
//scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b + , b + + n);
for (int i = ; i <= n; i++)
a[i] = lower_bound(b + , b + + n, a[i]) - b;
// cout << -1 << endl;
for (int i = ; i <= n; i++)
{
// cout << a[i] << endl;
dp[i][] = ;
for (int j = ; j <= i; j++)
dp[i][j] = Sum(j - , a[i] - );
for (int j = ; j <= i; j++)
Update(j, a[i], dp[i][j]);
}
long long ans = ;
for (int i = ; i <= n; i++)
{
ans = (ans + dp[i][m]);
while(ans>=mod)
ans-=mod;// % mod;
//cout << dp[i][m] << endl;
}
printf("Case #%d: %lld\n", ++cas, ans);
}
return ;
}