結果

問題 No.366 ロボットソート
ユーザー alpha_virginisalpha_virginis
提出日時 2016-08-21 15:57:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 961 bytes
コンパイル時間 1,449 ms
コンパイル使用メモリ 168,264 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 15:28:58
合計ジャッジ時間 2,299 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

int bubble(std::vector<int>& xs) {
  int n = xs.size();
  int res = 0;
  for(int i = 1; i < n; ++i) {
    for(int j = n - 1; j >= i; --j) {
      if( xs[j-1] > xs[j] ) {
        res += 1;
        std::swap(xs[j-1], xs[j]);
      }
    }
  }
  return res;
}

bool sorted(std::vector<int>& xs) {
  int n = xs.size();
  for(int i = 1; i < n; ++i) {
    if( xs[i-1] > xs[i] ) return false;
  }
  return true;
}

int main() {
  int n; scanf("%d", &n);
  int k; scanf("%d", &k);
  int xs[1024];
  for(int i = 0; i < n; ++i) {
    scanf("%d", &xs[i]);
  }

  std::vector<int> array[1024];
  for(int i = 0; i < n; ++i) {
    array[i % k].push_back(xs[i]);
  }
  int res = 0;
  for(int i = 0; i < k; ++i) {
    res += bubble(array[i]);
  }
  std::vector<int> ys;
  for(int i = 0; i < n; ++i) {
    ys.push_back(array[i%k][i/k]);
  }

  if( not sorted(ys) ) {
    printf("-1\n");
    return 0;
  }
  printf("%d\n", res);
  
  
  
  return 0;
}

0