結果

問題 No.366 ロボットソート
コンテスト
ユーザー Tiến Mạnh Trần
提出日時 2026-02-10 17:30:01
言語 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  
実行時間 -
コード長 959 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,332 ms
コンパイル使用メモリ 231,852 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-02-10 17:30:06
合計ジャッジ時間 3,689 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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> b = a;
    sort(b.begin(), b.end());

    unordered_map<long long, vector<int>> pos;
    for(int i = 0; i < n; i++)
        pos[b[i]].push_back(i);

    unordered_map<long long, int> idx;
    vector<int> to(n);

    for(int i = 0; i < n; i++) {
        int p = pos[a[i]][idx[a[i]]++];
        if(p % k != i % k) {
            cout << -1;
            return 0;
        }
        to[i] = p;
    }

    vector<bool> vis(n, false);
    int ans = 0;

    for(int i = 0; i < n; i++) {
        if(vis[i] || to[i] == i) continue;

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

    cout << ans;
}
0