Coldfresh的博客

keeeeeep!~

The Battle of Chibi HDU - 5542 (数状数组+dp)

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 NN information to be leaked, in happening order. Each of the information was estimated to has aiai 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 MM information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the NN information and just select MM 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(1≤100). TT test cases follow.

Each test case begins with two numbers N(1≤N≤103)N(1≤N≤103) and M(1≤M≤N)M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then NN numbers in a line, the ithith number ai(1≤ai≤109)ai(1≤ai≤109) indicates the value in Cao Cao’s opinion of the ithith information in happening order.
Output
For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy 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)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.

思路:这题是用的dp,朴素做法就是三次for循环的O(n3)的做法,但是这个发现只是和数的大小有关,而与具体数值有关,所以可以用离散化,然后用树状数组优化求和,因为每次都新加就一个候选变量。

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cmath>
#define maxx 1005
#define mod 1000000007
using namespace std;
long long c[maxx];
int n,m;
void change(int x,long long p)
{
    for(;x<=n;x+=x&-x)c[x]=(c[x]+p)%mod;
}
long long ask(int x)
{
    long long ans=0;
    for(;x;x-=x&-x)ans=(ans+c[x])%mod;
    return ans;
}
int dp[1005][1005];
int a[1005];
int id[1005];
int num;
int get(int x)
{
    return lower_bound(id+1,id+1+num,x)-id+1;
}
int main()
{
    int t;
    cin>>t;
    int cal=1;
    while(t--)
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            scanf("%d",a+i),id[i]=a[i];
        sort(id+1,id+n+1);
        num=unique(id+1,id+1+n)-id-1;
        for(int i=1;i<=n;i++)
            a[i]=get(a[i]);
        a[0]=1;
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1;i<=m;i++)
        {
            memset(c,0,sizeof(c));
            change(a[0],dp[i-1][0]);
            for(int j=1;j<=n;j++)
            {
                dp[i][j]=(int)ask(a[j]-1);
                change(a[j],dp[i-1][j]);
            }
        }
        int ans=0;
        for(int i=1;i<=n;i++)
            ans=(ans+dp[m][i])%mod;
        printf("Case #%d: %d\n",cal++,ans);
    }
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coldfresh/article/details/79963871
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

The Battle of Chibi HDU - 5542 (数状数组+dp)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭