結果

問題 No.366 ロボットソート
コンテスト
ユーザー Tiến Mạnh Trần
提出日時 2026-02-10 17:34:28
言語 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  
実行時間 -
コード長 989 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,337 ms
コンパイル使用メモリ 232,024 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-02-10 17:34:31
合計ジャッジ時間 3,629 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 8 WA * 15
権限があれば一括ダウンロードができます

ソースコード

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;
    vector<long long> a(n);
    for(int i=0;i<n;i++) cin>>a[i];
    cin >> k;

    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]]++];
        // check modulo class
        if(p % k != i % k){
            cout << -1 << "\n";
            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 cur=i, len=0;
        while(!vis[cur]){
            vis[cur]=true;
            cur = to[cur];
            len++;
        }
        ans += (len-1);
    }

    cout << ans << "\n";
    return 0;
}
0