結果

問題 No.366 ロボットソート
ユーザー CleyLCleyL
提出日時 2020-03-05 23:39:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 752 bytes
コンパイル時間 909 ms
コンパイル使用メモリ 74,952 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 03:37:06
合計ジャッジ時間 1,372 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int ans = 0;
void Bsort(vector<int>::iterator f, vector<int>::iterator l){
  for(auto e = f; e != l; e++){
    for(auto i = l-1; i != e; i--){
      auto j = i;
      j--;
      if(*i < *j){
        iter_swap(i,j);
        ans++;
      }
    }
  }
}
int main(){
  int n,k;cin>>n>>k;
  vector<int> A[min(n,k)];
  for(int i = 0; n > i; i++){
    int t;cin>>t;
    A[i%k].push_back(t);
  }
  for(int i = 0; min(n,k) > i; i++){
    Bsort(A[i].begin(),A[i].end());
  }
  vector<int> B(n);
  for(int i = 0; n > i; i++){
    B[i] = A[i%k][i/k];
  }
  for(int i = 1; n > i; i++){
    if(B[i-1] > B[i]){
      cout << -1 << endl;
      return 0;
    }
  }
  cout << ans << endl;

}
0