結果

問題 No.366 ロボットソート
コンテスト
ユーザー nguyen hùng
提出日時 2026-02-10 17:11:06
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
WA  
実行時間 -
コード長 1,475 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,445 ms
コンパイル使用メモリ 232,516 KB
実行使用メモリ 7,976 KB
最終ジャッジ日時 2026-02-10 17:11:10
合計ジャッジ時間 4,200 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;

    vector<long long> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    vector<long long> clone = a;
    sort(clone.begin(), clone.end());

    long long ans = 0;

    for (int r = 0; r < k; r++) {
        vector<long long> cur, target;

        for (int i = r; i < n; i += k) {
            cur.push_back(a[i]);
            target.push_back(clone[i]);
        }

        auto c1 = cur, c2 = target;
        sort(c1.begin(), c1.end());
        sort(c2.begin(), c2.end());
        if (c1 != c2) {
            cout << -1;
            return 0;
        }

        int m = cur.size();

        vector<long long> sorted_cur = cur;
        sort(sorted_cur.begin(), sorted_cur.end());

        unordered_map<long long, queue<int>> pos;
        for (int i = 0; i < m; i++) {
            pos[sorted_cur[i]].push(i);
        }

        vector<int> p(m);
        for (int i = 0; i < m; i++) {
            p[i] = pos[cur[i]].front();
            pos[cur[i]].pop();
        }

        vector<bool> vis(m, false);
        for (int i = 0; i < m; i++) {
            if (vis[i] || p[i] == i) continue;

            int u = i, len = 0;
            while (!vis[u]) {
                vis[u] = true;
                u = p[u];
                len++;
            }
            ans += len - 1;
        }
    }

    cout << ans;
    return 0;
}
0