bzoj2431
dp
设dp[i][j]为当前到了第i个位置,一共出现了j个逆序对,然后考虑转移,因为前面只有i-1个数,所以这位只能产生[0,i-1]对逆序对,那么dp[i][j]=sigma(dp[i-1][k]),k∈[i-j+1,j]。
但是这样直接搞是O(n^3)的,那么我们用前缀和优化一下,就是O(n^2)的了。
碰见这种排列的题不能直接把现在填的数设为状态,我们可以通过调整之前填的数使当前要放进去的数是合法的,所以之前的数就是不确定的,应该考虑用其他条件去设状态
#include<bits/stdc++.h> using namespace std; const int N = 1010; namespace IO { const int Maxlen = N * 50; char buf[Maxlen], *C = buf; int Len; inline void read_in() { Len = fread(C, 1, Maxlen, stdin); buf[Len] = '\0'; } inline void fread(int &x) { x = 0; int f = 1; while (*C < '0' || '9' < *C) { if(*C == '-') f = -1; ++C; } while ('0' <= *C && *C <= '9') x = (x << 1) + (x << 3) + *C - '0', ++C; x *= f; } inline void read(int &x) { x = 0; int f = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } x *= f; } inline void read(long long &x) { x = 0; long long f = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = (x << 1ll) + (x << 3ll) + c - '0'; c = getchar(); } x *= f; } } using namespace IO; int n, k; int dp[N][N]; int main() { read(n); read(k); for(int i = 0; i <= k; ++i) dp[0][i] = 1; for(int i = 1; i <= n; ++i) for(int j = 0; j <= k; ++j) { dp[i][j] = dp[i - 1][j]; if(j >= i) dp[i][j] = dp[i][j] - dp[i - 1][j - i]; if(j) dp[i][j] = dp[i][j] + dp[i][j - 1]; if(dp[i][j] >= 10000) dp[i][j] -= 10000; if(dp[i][j] < 0) dp[i][j] += 10000; } if(k == 0) puts("1"); else printf("%d\n", ((dp[n][k] - dp[n][k - 1]) % 10000 + 10000) % 10000); return 0; }