結果

問題 No.1854 Limited Bubble Sort
ユーザー 👑 NachiaNachia
提出日時 2022-02-25 21:57:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 1,961 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 83,772 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-16 16:50:58
合計ジャッジ時間 2,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
using i64 = long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)


pair<bool, vector<int>> solve(vector<int> P){
    int n = P.size();
    for(int p : P) if(p < 0) return { false, {} };
    for(int p : P) if(p >= n) return { false, {} };
    vector<int> res;
    auto op = [&](int q) -> void {
        assert(P[q] != q);
        assert(P[q+1] != q+1);
        swap(P[q],P[q+1]);
        res.push_back(q);
    };
    for(int q=0; q<n; q++){
        int p = -1;
        for(int i=q; i<n; i++) if(P[i] == q) p = i;
        if(p == -1) return { false, {} };
        while(p != q){
            int nx = p-1;
            while(q <= nx && P[nx] == nx+1) nx--;
            if(q <= nx) for(int i=nx; i<p-1; i++) op(i);
            else nx = q;
            for(int i=p-1; i>=nx; i--) op(i);
            p = nx;
        }
    }
    //for(auto a : P) cout << a << " ";
    //cout << endl;
    return make_pair( true, move(res) );
}


void testcase(){
    int N; cin >> N;
    vector<int> P(N); rep(i,N){ int p; cin >> p; p--; P[i] = p; }
    vector<int> coins;
    coins.push_back(-1);
    rep(i,N) if(P[i] == i) coins.push_back(i);
    coins.push_back(N);
    vector<int> ans;
    rep(i,coins.size()-1){
        int l = coins[i]+1, r = coins[i+1];
        vector<int> tmp;
        for(int j=l; j<r; j++) tmp.push_back(P[j] - l);
        if(tmp.empty()) continue;
        auto [c,restmp] = solve(tmp);
        if(!c){ cout << "-1\n"; return; }
        for(auto a : restmp) ans.push_back(l+1+a);
    }
    cout << ans.size() << '\n';
    rep(i,ans.size()){
        if(i) cout << ' ';
        cout << ans[i];
    }
    cout << '\n';
}

int main(){
    int T; cin >> T;
    while(T--) testcase();
    return 0;
}




struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0