結果

問題 No.1854 Limited Bubble Sort
ユーザー KudeKude
提出日時 2022-02-25 23:43:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,985 bytes
コンパイル時間 2,289 ms
コンパイル使用メモリ 225,904 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-16 19:17:42
合計ジャッジ時間 6,048 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) { a = max(a, b); }
template<class T> void chmin(T& a, const T& b) { a = min(a, b); }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt;
  cin >> tt;
  while(tt--) {
    int n;
    cin >> n;
    VI p(n);
    rep(i, n) cin >> p[i], p[i] -= i + 1;
    bool ok = true;
    rep(i, n) {
      if (p[i] < 0) {
        for(int j = i + p[i] + 1; j < i; j++) {
          if (p[j] == 0) ok = false;
        }
      } else if (p[i] > 0) {
        for(int j = i + 1; j < i + p[i]; j++) {
          if (p[j] == 0) ok = false;
        }
      }
    }
    if (!ok) {
      cout << -1 << '\n';
      continue;
    }
    VI ans;
    rep(i, n) {
      if (p[i] == 0) continue;
      int t = -1;
      for(int j = i; j < n; j++) {
        if (j + p[j] == i) {
          t = j;
          break;
        }
      }
      while(t > i) {
        int k = t - 1;
        while(k >= i) {
          if (p[k] != -1) break;
          k--;
        }
        if (k == i - 1) {
          for(; t > i; t--) {
            ans.emplace_back(t - 1);
            swap(p[t - 1], p[t]);
            p[t - 1]--;
            p[t]++;
          }
        } else {
          ans.emplace_back(k);
          swap(p[k], p[k + 1]);
          p[k]--;
          p[k]++;
          if (k == t - 1) t = k;
        }
      }
    }
    int sz = ans.size();
    cout << sz << '\n';
    rep(i, sz) cout << ans[i] + 1 << " \n"[i + 1 == sz];
  }
}
0