結果

問題 No.366 ロボットソート
ユーザー hogeover30
提出日時 2016-04-29 23:15:36
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
MLE  
実行時間 -
コード長 684 bytes
コンパイル時間 807 ms
コンパイル使用メモリ 84,456 KB
実行使用メモリ 628,392 KB
最終ジャッジ日時 2024-10-04 18:51:41
合計ジャッジ時間 4,447 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10 MLE * 1 -- * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;

int main()
{
    int n, k; cin>>n>>k;
    vector<int> a(n);
    for(int& e: a) cin>>e;

    queue<vector<int>> q;
    map<vector<int>, int> v;
    q.push(a);
    v[a]=0;

    while (q.size()) {
        auto p=q.front(); q.pop();
        if (is_sorted(begin(p), end(p))) {
            cout<<v[p]<<endl;
            return 0;
        }
        int x=v[p];
        for(int i=0; i+k<n; ++i) {
            swap(p[i], p[i+k]);
            if (!v.count(p)) {
                v[p]=x+1;
                q.push(p);
            }
            swap(p[i], p[i+k]);
        }
    }
    cout<<-1<<endl;
}
0