結果

問題 No.366 ロボットソート
コンテスト
ユーザー Tiến Mạnh Trần
提出日時 2026-02-10 17:55:42
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,228 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,459 ms
コンパイル使用メモリ 229,444 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-02-10 17:55:46
合計ジャッジ時間 3,520 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 8 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9+7;
const int maxn = 1e5 + 1;
#define pii pair<int, int>
#define ll long long
#define pq priority_queue
#define se second
#define fi first
int n, k;
vector<int> a;
void solve() {
    cin >> n;
    a.resize(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    cin >> k;
    vector<int> b = a;
    sort(b.begin(), b.end());
    unordered_map<ll, queue<int>> pos;
    for(int i = 0; i < n; i++){
        pos[b[i]].push(i);
    }
    vector<int> vitri(n);
    for (int i = 0; i < n; i++) {
        int p = pos[a[i]].front();
        pos[a[i]].pop();
        if(p%k != i%k) {
            cout << -1;
            return;
        }
        vitri[i] = p;
    }
    vector<bool> gaplai(n, false);
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if(gaplai[i] || vitri[i] == i) continue;
        int len = 0, cur = i;
        while(!gaplai[cur]) {
            gaplai[cur] = true;
            len++;
            cur = vitri[cur];
        }
        if(n%2 == 0) ans += len;
        else ans += len - 1;
    }
    cout << ans << '\n';
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    solve();
    return 0;
}
0