結果
| 問題 |
No.2898 Update Max
|
| コンテスト | |
| ユーザー |
vjudge1
|
| 提出日時 | 2024-09-26 00:40:15 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,611 bytes |
| コンパイル時間 | 1,905 ms |
| コンパイル使用メモリ | 196,204 KB |
| 最終ジャッジ日時 | 2025-02-24 12:42:10 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 2 |
| other | AC * 6 WA * 22 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int mod = 998244353;
int n, a[N];
bool vis[N];
vector<int> s;
long long pl[N], inv[N], ipl[N];
void init(int inf){
pl[0] = pl[1] = 1, inv[0] = inv[1] = 1, ipl[0] = ipl[1] = 1;
for(int i = 2; i <= inf; i++){
pl[i] = pl[i - 1] * i % mod;
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
ipl[i] = ipl[i - 1] * inv[i] % mod;
}
}
long long A(long long n, long long m){
if(m > n) return 0;
if(n < 0 || m < 0) return 0;
return pl[n] * ipl[n - m] % mod;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
int sum = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
if(a[i] >= 1) vis[a[i]] = 1;
sum += (a[i] < 0);
}
init(n);
for(int i = 1; i <= n; i++){
if(!vis[i]) s.push_back(i);
}
long long ans = 0;
for(int i = 1, cnt = 0, maxx = 0; i <= n; i++){
if(a[i] >= 0){
if(a[i] > maxx){
int l = lower_bound(s.begin(), s.end(), a[i]) - s.begin();
ans = (ans + A(l, cnt) * pl[sum - cnt] % mod) % mod;
}
}
else{
/*for(int j = lower_bound(s.begin(), s.end(), maxx + 1) - s.begin(); j < s.size(); j++){
ans = (ans + A(j, cnt) * pl[sum - cnt - 1] % mod) % mod;
}*/
int l = lower_bound(s.begin(), s.end(), maxx + 1) - s.begin() - 1;
ans = (ans + (A(s.size(), cnt + 1) - A(l, cnt + 1) + mod) % mod * ipl[cnt] % mod * pl[sum - cnt - 1] % mod) % mod;
}
cnt += (a[i] < 0), maxx = max(maxx, a[i]);
}
cout << ans;
return 0;
}
/*
4
1 -1 3 -1
6
2 4 5 3 6 1
17
-1 -1 8 2 10 9 12 -1 7 -1 -1 -1 11 -1 13 -1 -1
*/
vjudge1