結果
問題 |
No.2382 Amidakuji M
|
ユーザー |
|
提出日時 | 2023-07-14 21:40:04 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 682 bytes |
コンパイル時間 | 3,958 ms |
コンパイル使用メモリ | 251,740 KB |
最終ジャッジ日時 | 2025-02-15 13:47:41 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 2 RE * 17 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; int main() { int N, M; cin >> N >> M; vector<int> P(N); for (int i = 0; i < N; i++){ cin >> P[i]; P[i]--; } atcoder::fenwick_tree<int> fw(N); long long c = 0; for (int i = 0; i < N; i++){ c += fw.sum(P[i], N); fw.add(P[i], 1); } int ans = 0; if (c % M == 0){ ans = c; } else if (c < M){ if ((M - c) % 2 == 0){ ans = M; } else { if (M % 2 == 1){ ans = M * 2; } else { ans = -1; } } } else { M = (c + M - 1) / M * M; if ((M - c) % 2 == 0){ ans = M; } else { if (M % 2 == 1){ ans = M * 2; } else { ans = -1; } } } cout << ans << '\n'; }