UESTC 1217 The Battle of Chibi (树状数组 + 离散化 + 动态规划)

C - The Battle of Chibi
Time Limit:4000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu
Submit  Status  Practice  UESTC 1217

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 $a_i$ 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\leq 100$). $T$ test cases follow.

Each test case begins with two numbers $N$($1\leq N\leq 10^3$) and $M$($1\leq M\leq N$), indicating the number of information and number of information Gai Huang will select. 
Then $N$ numbers in a line, the $i_{th}$ number $a_i$($1\leq a_i\leq 109$) indicates the value in Cao Cao's opinion of the $i_{th}$ 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$($10^9+7$).

Sample Input


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.

经验总结:对于需要比较大小的求和可以使用树状数组,他的求和时间复杂度为O(logn),
所以对于此题而言,我们开始可以很简单的列出状态转移方程
dp[i][j]代表以i结尾的长度为j的数目
状态转移方程:dp[i][j] = sum{dp[k][j - 1]},X[k] < X[i]
此处还需要对输入的数据进行离散化,新建一个数组排序一下即可

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;


#define pb push_back
#define mp make_pair
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("D://imput.txt", "r", stdin)

typedef long long LL;
typedef pair<int, int > PII;
typedef unsigned long long uLL;
template<typename T>
void print(T* p, T* q, string Gap = " ") {
    int d = p < q ? 1 : -1;
    while(p != q) {
        cout << *p;
        p += d;
        if(p != q) cout << Gap;
    }
    cout << endl;
}
template<typename T>
void print(const T &a, string bes = "") {
    int len = bes.length();
    if(len >= 2)cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

const int INF = 0x3f3f3f3f;
const int MAXM = 2e5;
const int MAXN = 1e3 + 5;
const int mod = 1e9 + 7;
int T;
int N, M;
int X[MAXN],XA[MAXN];
LL dp[MAXN][MAXN];
int binary_searchs(int x){
    int l = 0,r = N;
    while(r - l > 1){
        int mid = (r + l) >> 1;
        if(XA[mid] >= x) r = mid;
        else l = mid;
    }
    return r;
}

int lowbit(int x){
    return x & (-x);
}

void add(int pos, int len,LL d){
    while(pos <= N){
        dp[pos][len] += d;
        pos += lowbit(pos);
    }
}

LL sum(int pos, int len){
    LL ret = 0;
    while(pos > 0){
        ret += dp[pos][len];
        ret %= mod;
        pos -= lowbit(pos);
    }
    return ret;
}

int main(){
    int cas = 1;
    scanf("%d", &T);
    while(T --){
        memset(dp, 0, sizeof(dp));
        scanf("%d%d", &N, &M);
        for(int i = 1;i <= N;i ++){
            scanf("%d", &X[i]);
            XA[i] = X[i];
        }
        sort(XA + 1, XA + N + 1);
        for(int i = 1;i <= N;i ++){
            int id = binary_searchs(X[i]);
            add(id, 1, 1);
            for(int j = 1;j <= M;j ++){
                add(id, j, sum(id - 1, j - 1));
            }
        }
        printf("Case #%d: %lld\n", cas ++, sum(N, M));
    }
    return 0;
}


你可能感兴趣的:(UESTC 1217 The Battle of Chibi (树状数组 + 离散化 + 动态规划))