結果

問題 No.2616 中央番目の中央値
ユーザー vjudge1vjudge1
提出日時 2024-11-12 01:32:49
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 173 ms / 2,000 ms
コード長 1,213 bytes
コンパイル時間 2,709 ms
コンパイル使用メモリ 214,012 KB
実行使用メモリ 15,232 KB
最終ジャッジ日時 2024-11-12 01:32:56
合計ジャッジ時間 7,046 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second

const int N=3e5+10;
const ll mod=998244353;
int p[N];

ll fpow(ll x, int k){
    ll res=1;
    while(k){
        if (k&1) res=res*x%mod;
        x=x*x%mod;
        k>>=1;
    }
    return res;
}

ll inv[N<<1], fac[N<<1];
ll C(int n, int k){return fac[n]*inv[k]%mod*inv[n-k]%mod;}

int n;
int bit[N];
void update(int x){for(;x<=n;x+=x&-x) bit[x]++;}
int query(int x){
    int res=0;
    for(;x;x-=x&-x) res+=bit[x];
    return res;
}

void _solve(){
    cin >> n;
    fac[0]=inv[0]=1;
    for(int i=1;i<=(n<<1);i++){
        fac[i]=fac[i-1]*i%mod;
        inv[i]=fpow(fac[i], mod-2);
    }
    for(int i=1;i<=n;i++) cin >> p[i];
    ll ans=0;
    for(int i=1;i<=n;i++){
        int b=query(p[i]);
        int a=i-b-1;
        int d=p[i]-b-1;
        int c=n-i-d;
        ans+=C(a+d, min(a, d))*C(b+c, min(b, c))%mod;
        ans%=mod;
        update(p[i]);
    }
    cout << ans << '\n';
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int _t=1;
    //cin >> _t;
    while(_t--) _solve();
}
0