結果

問題 No.2616 中央番目の中央値
ユーザー startcppstartcpp
提出日時 2022-10-02 10:24:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 1,436 bytes
コンパイル時間 638 ms
コンパイル使用メモリ 73,148 KB
実行使用メモリ 26,768 KB
最終ジャッジ日時 2023-12-21 23:47:10
合計ジャッジ時間 5,163 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,796 KB
testcase_01 AC 4 ms
9,796 KB
testcase_02 AC 3 ms
9,796 KB
testcase_03 AC 4 ms
9,796 KB
testcase_04 AC 3 ms
9,796 KB
testcase_05 AC 4 ms
9,796 KB
testcase_06 AC 3 ms
9,796 KB
testcase_07 AC 3 ms
9,796 KB
testcase_08 AC 3 ms
9,796 KB
testcase_09 AC 3 ms
9,796 KB
testcase_10 AC 3 ms
9,796 KB
testcase_11 AC 3 ms
9,796 KB
testcase_12 AC 4 ms
9,796 KB
testcase_13 AC 4 ms
9,924 KB
testcase_14 AC 5 ms
10,052 KB
testcase_15 AC 7 ms
10,308 KB
testcase_16 AC 12 ms
12,612 KB
testcase_17 AC 16 ms
13,124 KB
testcase_18 AC 24 ms
14,276 KB
testcase_19 AC 46 ms
16,964 KB
testcase_20 AC 47 ms
16,964 KB
testcase_21 AC 97 ms
21,572 KB
testcase_22 AC 152 ms
26,768 KB
testcase_23 AC 175 ms
26,720 KB
testcase_24 AC 127 ms
26,720 KB
testcase_25 AC 127 ms
26,720 KB
testcase_26 AC 152 ms
26,720 KB
testcase_27 AC 148 ms
26,720 KB
testcase_28 AC 146 ms
26,720 KB
testcase_29 AC 168 ms
26,720 KB
testcase_30 AC 162 ms
26,720 KB
testcase_31 AC 150 ms
26,720 KB
testcase_32 AC 148 ms
26,720 KB
testcase_33 AC 148 ms
26,720 KB
testcase_34 AC 148 ms
26,720 KB
testcase_35 AC 184 ms
26,720 KB
testcase_36 AC 146 ms
26,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <atcoder/fenwicktree>
#define int long long
using namespace std;
using namespace atcoder;

//参考: https://drken1215.hatenablog.com/entry/2018/06/08/210000
const int mod = 998244353;
int fact[300001], inv[300001], finv[300001];
void comb_init(int n) {
	fact[0] = fact[1] = finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i <= n; i++) {
		fact[i] = fact[i - 1] * i % mod;
		inv[i] = (mod - inv[mod % i] * (mod / i) % mod) % mod;
		finv[i] = finv[i - 1] * inv[i] % mod;
	}
}

int comb(int n, int k) {
	return fact[n] * finv[k] % mod * finv[n - k] % mod;
}

int n;
int p[300000];

signed main() {
	int i;
	
	cin >> n;
	comb_init(n);
	for (i = 0; i < n; i++) { cin >> p[i]; p[i]--; }
	
	fenwick_tree<int> fwL(n), fwR(n);
	vector<int> A(n), B(n), C(n), D(n);
	for (i = 0; i < n; i++) { //p[i]より左の要素について、p[i]未満・p[i]超えの個数をカウント
		A[i] = fwL.sum(0, p[i]);	//値が[0, p[i])に含まれる要素数
		B[i] = i - A[i];
		fwL.add(p[i], 1);
	}
	
	for (i = n - 1; i >= 0; i--) { //p[i]より右の要素について、p[i]未満・p[i]超えの個数をカウント
		C[i] = fwR.sum(0, p[i]);	//値が[0, p[i])に含まれる要素数
		D[i] = (n - 1 - i) - C[i];
		fwR.add(p[i], 1);
	}
	
	int ans = 0;
	for (i = 0; i < n; i++) {
		int res = comb(A[i] + D[i], A[i]) * comb(B[i] + C[i], B[i]) % mod;
		ans += res;
		ans %= mod;
	}
	
	cout << ans << endl;
	return 0;
}
0