传送门:点击打开链接
题意:求长度为n的排列的逆序对等于k的排列总数,取模1e9+7
思路:刚开始想找规律,但是发现根本没法搞。。
但是我们如果从dp的角度去思考,就能找到一个dp是这样的
dp[n][k]=dp[n-1][i],max(k-n+1,0)<=i<=k
其中边界dp[0][0]=1
那么就能用离线+前缀和去搞答案了
要注意的就是ans等于0的情况,离线的时候要注意p是否能取到所有的,不然可能后面的答案会全错
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;
const int MX = 2e4 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
struct Data {
int id, n, k;
bool operator<(const Data &P) const {
if(n == P.n) return k < P.k;
return n < P.n;
}
} A[MX];
int n, ans[MX];
LL dp[2][MX];
int main() {
//FIN;
while(~scanf("%d", &n)) {
memset(ans, 0, sizeof(ans));
int mn = 0, mk = 0;
for(int i = 1; i <= n; i++) {
A[i].id = i;
scanf("%d%d", &A[i].n, &A[i].k);
mn = max(mn, A[i].n);
mk = max(mk, A[i].k);
}
sort(A + 1, A + 1 + n);
int cur = 0, nxt = 1, p = 1;
for(int i = 0; i <= mk; i++) dp[cur][i] = 1;
for(int i = 1; i <= mn; i++) {
int bound = i * (i - 1) / 2;
for(int j = 0; j <= min(mk, bound); j++) {
int l = max(j - i + 1, 0), r = j;
LL sl = l == 0 ? 0 : dp[cur][l - 1];
dp[nxt][j] = (dp[cur][r] - sl + mod) % mod;
while(p <= n && i == A[p].n && j == A[p].k) {
ans[A[p++].id] = dp[nxt][j];
}
dp[nxt][j] = (dp[nxt][j] + dp[nxt][j - 1]) % mod;
}
for(int j = min(mk, bound) + + 1; j <= mk; j++) {
dp[nxt][j] = dp[nxt][j - 1];
while(p <= n && i == A[p].n && j == A[p].k) {
ans[A[p++].id] = 0;
}
}
swap(cur, nxt);
}
for(int i = 1; i <= n; i++) {
printf("%d\n", ans[i]);
}
}
return 0;
}