結果

問題 No.366 ロボットソート
ユーザー kappybarkappybar
提出日時 2020-05-10 11:55:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,058 bytes
コンパイル時間 1,834 ms
コンパイル使用メモリ 178,964 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 18:37:15
合計ジャッジ時間 3,438 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 7 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;


struct Unionfind{
    vector<int> parents;
    int m;
    Unionfind(int n):parents(n,-1){
        m = n;
    }

    int find(int x){
        if(parents.at(x) < 0){
            return x;
        }else{
            parents.at(x) = find(parents.at(x));
            return parents.at(x);
        }
    }

    void unite(int x,int y){
        x = find(x);
        y = find(y);

        if(x==y) return;
        if(parents.at(x) > parents.at(x))swap(x,y);
        parents.at(x) += parents.at(y);
        parents.at(y) = x;
    }  

   int size(int x){
       return -parents.at(find(x));
   }

   bool same(int x,int y){
       return find(x) ==find(y);
   }

   vector<int> members(int x){
       int root = find(x);
       vector<int> A;
       for(int i=0;i<m;i++){
           if(find(i) ==root ){
               A.push_back(i);
           }
       }
      return A;
  }
};

int main(){
    int n,k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    Unionfind uf(n);
    rep(i,n){
        uf.unite(i,i%k);
    }
    vector<P> A(n);
    rep(i,n){
        A[i].first = a[i];
        A[i].second = i;
    }
    sort(A.begin(),A.end());
    bool ok = true;
    rep(i,n){
        if(!uf.same(A[i].second,i)) ok = false;
    }
    if(!ok){
        cout << -1 << endl;
        return 0;
    }

    int cnt = 0;
    vector<vector<int>> b(k,vector<int>());

    rep(i,n){
        b[i%k].push_back(a[i]);
    }

    rep(i,k){
        while(1){
            bool update = false;
            rep(j,b[i].size()-1){
                if(b[i][j] > b[i][j+1]){
                    swap(b[i][j],b[i][j+1]);
                    update = true;
                    cnt ++;
                }
            }
            if(!update) break;
        }
    }
    cout << cnt << endl;
    return 0;
}
0