結果

問題 No.366 ロボットソート
ユーザー shora_kujira16shora_kujira16
提出日時 2016-04-29 22:43:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 876 bytes
コンパイル時間 1,695 ms
コンパイル使用メモリ 174,140 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 15:10:39
合計ジャッジ時間 2,826 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define DEBUG(x) cerr << #x << " = " << x << endl

bool check(vector<int> A, vector<vector<int>> S, int K) {
  sort(A.begin(), A.end());
  REP(i,S.size()) {
    sort(S[i].begin(), S[i].end());
  }
  REP(i,A.size()) {
    if(A[i] != S[i % K][i / K]) return false;
  }
  return true;
}

int bubble_sort(const vector<int>& ary) {
  int ans = 0;
  REP(i,ary.size()) {
    REP(j,i) {
      if(ary[j] > ary[i]) ans++;
    }
  }
  return ans;
}

signed main() {
  int N, K; cin >> N >> K;
  vector<int> A(N);
  REP(i,N) cin >> A[i];
  vector<vector<int>> S(K);
  REP(i,N) {
    S[i % K].push_back(A[i]);
  }
  if(check(A, S, K) == false) {
    cout << -1 << endl;
  }
  else {
    int ans = 0;
    REP(i,K) ans += bubble_sort(S[i]);
    cout << ans << endl;
  }
}
0