hdu5542 树状数组优化dp

南阳比赛的题目,队友一发AC,我也来试试

题意很容易想到n^3的动态规划,dp[i][j]表示到i位置取j长度的种类,这样dp[i][j] = sum( dp[k][j-1], iff a[k] < a[i], 0<=k

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define N 1005
#define MOD 1000000007
using namespace std;

int a[N], dp[N][N], num;
int t[N][N];

inline int lowbit(int x) {
    return x & (-x);
}
void add(int i, int j, int c) {
    while(j < num) {
        t[i][j] += c;
        if( t[i][j] >= MOD) t[i][j] %= MOD;
        j += lowbit(j);
    }
    return;
}
int sum(int i, int j) {
    int re = 0;
    while(j) {
        re += t[i][j];
        if(re >= MOD) re %= MOD;
        j -= lowbit(j);
    }
    return re;
}
int main(int argc, char* argv[]) {
    int n, m;
    int tt, ca = 1;
    scanf("%d", &tt);
    while( tt-- ) {
        scanf("%d%d", &n, &m);
        set s;
        map mp;
        for(int i=0; i::iterator it = s.begin(); it != s.end(); ++it) {
            mp[ *it ] = num++;
        }
        memset(dp, 0, sizeof(dp));
        memset(t, 0, sizeof(t));
        for(int i=0; i= MOD) dp[i][j] %= MOD;

        //    printf("%d %d %d\n", i, j, dp[i][j]);
            add(j, mp[ a[i] ], dp[i][j]);
        }
        int re = 0;
        for(int i=0; i= MOD) re %= MOD;
        }
        printf("Case #%d: %d\n", ca++, re);
    }
    return 0;
}


你可能感兴趣的:(hdu)