結果
| 問題 | No.366 ロボットソート |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-02-10 17:11:38 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,204 bytes |
| 記録 | |
| コンパイル時間 | 3,350 ms |
| コンパイル使用メモリ | 351,816 KB |
| 実行使用メモリ | 7,976 KB |
| 最終ジャッジ日時 | 2026-02-10 17:11:43 |
| 合計ジャッジ時間 | 4,680 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 WA * 8 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
vector<int> a(n);
for(int i = 0; i < n; i++)
cin >> a[i];
vector<int> b = a;
sort(b.begin(), b.end());
// lưu tất cả vị trí của từng giá trị trong b
unordered_map<int, queue<int>> pos;
for(int i = 0; i < n; i++)
pos[b[i]].push(i);
vector<int> perm(n);
// build permutation toàn cục
for(int i = 0; i < n; i++){
if(pos[a[i]].empty()){
cout << -1;
return 0;
}
perm[i] = pos[a[i]].front();
pos[a[i]].pop();
if(perm[i] % k != i % k){
cout << -1;
return 0;
}
}
long long ans = 0;
vector<bool> visited(n, false);
// đếm cycle nhưng chỉ trong từng class mod k
for(int i = 0; i < n; i++){
if(visited[i]) continue;
int j = i;
int cycle = 0;
while(!visited[j]){
visited[j] = true;
j = perm[j];
cycle++;
}
if(cycle > 1)
ans += cycle - 1;
}
cout << ans;
}