【LOJ】#6432. 「PKUSC2018」真实排名

题解

简单分析一下,如果这个选手成绩是0,直接输出(inom{n}{k})

如果这个选手的成绩没有被翻倍,那么找到大于等于它的数(除了它自己)有a个,翻倍后不大于它的数有b个,那么就从这(a + b)个选手里找翻倍选手使得它排名不变
答案是(inom{a + b}{K})
如果这个选手成绩翻倍了,那么大于等于它的所有数,依旧大于它的有(c)个,然后剩余(a - c)个必须翻倍,剩下的翻不翻倍随意,所以答案是
(inom{N - (a - c) - 1}{K - (a - c) - 1})

代码

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define pdi pair<db,int>
#define mp make_pair
#define pb push_back
#define enter putchar('
')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;char c = getchar();T f = 1;
    while(c < '0' || c > '9') {
	if(c == '-') f = -1;
	c = getchar();
    }
    while(c >= '0' && c <= '9') {
	res = res * 10 + c - '0';
	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
	out(x / 10);
    }
    putchar('0' + x % 10);
}
const int MOD = 998244353;
int N,K,A[MAXN],val[MAXN];
int fac[MAXN],invfac[MAXN];
int inc(int a,int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
int fpow(int x,int c) {
    int res = 1,t = x;
    while(c) {
	if(c & 1) res = mul(res,t);
	t = mul(t,t);
	c >>= 1;
    }
    return res;
}
int C(int n,int m) {
    if(n < 0 || m < 0) return 0;
    if(n < m) return 0;
    return mul(fac[n],mul(invfac[n - m],invfac[m]));
}
void Solve() {
    read(N);read(K);
    for(int i = 1 ; i <= N ; ++i) {read(A[i]);val[i] = A[i];}
    sort(val + 1,val + N + 1);
    fac[0] = 1;
    for(int i = 1 ; i <= N ; ++i) fac[i] = mul(fac[i - 1],i);
    invfac[N] = fpow(fac[N],MOD - 2);
    for(int i = N - 1 ; i >= 0 ; --i) invfac[i] = mul(invfac[i + 1],i + 1);
    for(int i = 1 ; i <= N ; ++i) {
	if(A[i] == 0) {out(C(N,K));enter;continue;}
	int res = 0;
	int t = lower_bound(val + 1,val + N + 1,A[i]) - val;
	int s = lower_bound(val + 1,val + N + 1,A[i] % 2 == 0 ? A[i] / 2 : A[i] / 2 + 1) - val - 1;
	res = inc(res,C(N - t + s,K));
	int h = lower_bound(val + 1,val + N + 1,2 * A[i]) - val;
	h = N - h + 1;
	int d = N - t - h;
	res = inc(res,C(N - 1 - d,K - 1 - d));
	out(res);enter;
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
}
原文地址:https://www.cnblogs.com/ivorysi/p/10118268.html