title
HDU 5542
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.
Source
Recommend
wange2014 | We have carefully selected several similar problems for you: 6521 6520 6519 6518 6517
analysis
题目大意
在长度为n的序列中,长度为m的序列有多少种。
我们用 表示前 个数中,以 为结尾的长度为 的严格递增子序列的个数。
那么对于 ,我们只需要枚举所有小于 的 ,并且 ,将所有的 求和,可以得到 。
很容易想到 的算法,但是显然会超时,所以我们需要进行一些优化。
当我们枚举内层循环 时,外层循环 就可以被当成是定值。当 增加 , 的取值只是多了 这个新的决策。
因此我们用树状数组维护一个前缀和,表示 区间,长度为 时的方案数。
由于 时第一个参数放的是 ,也就是说现在直接用了他的权值作为了下标,所以只要下标比他小的,权值一定比他小,也就不需要进行比较了。直接去前缀和就可以了。
因为要用 的权值作为下标,但是 ,所以我们需要对 进行离散化,
最开始离散化用的 和 , 了,
后来改用了另一个数组 ,先 然后 ,并且直接把 的值改掉。
code
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+10,mod=1e9+7;
template<typename T>inline void read(T &x)
{
x=0;
T f=1, ch=getchar();
while (!isdigit(ch) && ch^'-') ch=getchar();
if (ch=='-') f=-1, ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
x*=f;
}
int n,m;
namespace BIT
{
int f[maxn][maxn];
inline int lowbit(int x)
{
return x & -x;
}
inline void add(int x,int y,int k)
{
while (x<=n)
{
(f[x][y]+=k)%=mod;
x+=lowbit(x);
}
}
inline int ask(int x,int y)
{
int ans=0;
while (x)
{
(ans+=f[x][y])%=mod;
x-=lowbit(x);
}
return ans;
}
}
using namespace BIT;
int a[maxn],b[maxn];
int main()
{
int t;read(t);
for (int kase=1; kase<=t; ++kase)
{
memset(f,0,sizeof(f));
read(n);read(m);
for (int i=1; i<=n; ++i) read(a[i]),b[i]=a[i];
sort(b+1,b+n+1);
for (int i=1; i<=n; ++i) a[i]=lower_bound(b+1,b+n+1,a[i])-b;
for (int i=1; i<=n; ++i)
for (int j=1; j<=min(i+1,m); ++j)
if (j==1) add(a[i],1,1);
else add(a[i],j,ask(a[i]-1,j-1));
printf("Case #%d: %d\n",kase,ask(n,m));
}
return 0;
}