結果
問題 | No.2382 Amidakuji M |
ユーザー |
|
提出日時 | 2023-07-14 21:43:22 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 77 ms / 2,000 ms |
コード長 | 694 bytes |
コンパイル時間 | 3,996 ms |
コンパイル使用メモリ | 252,412 KB |
最終ジャッジ日時 | 2025-02-15 13:50:59 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <bits/stdc++.h>#include <atcoder/all>using namespace std;int main() {long long 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);}long long 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';}