結果
問題 | No.2382 Amidakuji M |
ユーザー | Shell-Wataru |
提出日時 | 2023-07-14 21:51:40 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 70 ms / 2,000 ms |
コード長 | 1,684 bytes |
コンパイル時間 | 1,832 ms |
コンパイル使用メモリ | 133,760 KB |
実行使用メモリ | 7,936 KB |
最終ジャッジ日時 | 2024-09-16 06:47:21 |
合計ジャッジ時間 | 3,252 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#include <iostream> #include <algorithm> #include <vector> #include <deque> #include <queue> #include <set> #include <map> #include <limits> #include <cmath> #include <iomanip> #include <functional> #include <random> #include <set> #include <atcoder/string> #include <atcoder/math> #include <unordered_map> #include <climits> using namespace std; using ll = long long; using namespace atcoder; //zero indexed and vector template< typename T> class BIT { public: vector<T> data; BIT(long long n) : data(n) {} void add(int index, T v) { int N = data.size(); for (int i = index; i < N; i |= i + 1) { data[i] += v; } } long long sum(int index) { T retValue = 0; for (int i = index; i >= 0; i = (i & (i + 1)) - 1) { retValue += data[i]; } return retValue; } void show(){ for (auto v:data){ cout << v << endl; } } }; // 0~N-1に注意,compressするか確認 ll inversion_number(vector<ll> &A){ BIT<ll> bit(A.size()*2); ll ans = 0; for (int i = 0; i < A.size(); i++) { ans += i - bit.sum(A[i]); bit.add(A[i], 1); } return ans; } int solve() { ll N,M; cin >> N >> M; vector<ll> P(N); for(int i = 0;i < N;i++){ cin >> P[i]; P[i]--; } ll k = inversion_number(P); ll ans1 = (k+M-1)/M*M; ll ans2 = (k+M-1)/M*M + M; if( (ans1 - k) % 2 == 0){ cout << ans1 << endl; }else if((ans2- k) % 2 == 0){ cout << ans2 << endl; }else{ cout << -1 << endl; } return 0; } int main() { // ll T; // cin >> T; // while(T--){ solve(); // } return 0; }