結果

問題 No.366 ロボットソート
コンテスト
ユーザー Rumain831
提出日時 2026-12-03 17:59:12
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
+ 271µs
コード長 677 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,547 ms
コンパイル使用メモリ 192,796 KB
実行使用メモリ 9,872 KB
最終ジャッジ日時 2026-12-03 17:59:16
合計ジャッジ時間 3,902 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;

int f(vector<int>& a){
  int n=a.size(), ans=0;
  for(int i=0; i<n-1; i++)for(int j=i+1; j<n; j++) ans+=(a[i]>a[j]);
  return ans;
}

int main(void){
  int n, k; cin >> n >> k;
  vector<int> a(n), b;
  for(auto&x:a) cin >> x;
  b=a;
  sort(begin(b), end(b));
  int ans=0;
  for(int i=0; i<k; i++){
    vector<int> now;
    int id=i, c=0;
    while(id<n){
      now.push_back(a[id]);
      id+=k;
    }
    ans+=f(now);
    sort(begin(now), end(now));
    id=i;
    while(id<n) a[id]=now[c++], id+=k;
  }
  if(a==b) cout << ans << endl;
  else cout << -1 << endl;
  return 0; 
}
0