結果
| 問題 | No.366 ロボットソート |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-02-10 17:09:09 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,559 bytes |
| 記録 | |
| コンパイル時間 | 4,089 ms |
| コンパイル使用メモリ | 355,708 KB |
| 実行使用メモリ | 7,972 KB |
| 最終ジャッジ日時 | 2026-02-10 17:09:15 |
| 合計ジャッジ時間 | 5,469 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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());
long long ans = 0;
for (int start = 0; start < k; start++) {
vector<int> cur, need;
for (int i = start; i < n; i += k) {
cur.push_back(a[i]);
need.push_back(b[i]);
}
// check khả thi
vector<int> tmp1 = cur, tmp2 = need;
sort(tmp1.begin(), tmp1.end());
sort(tmp2.begin(), tmp2.end());
if (tmp1 != tmp2) {
cout << -1;
return 0;
}
int sz = cur.size();
// mapping value -> queue vị trí trong need
unordered_map<int, queue<int>> pos;
for (int i = 0; i < sz; i++) {
pos[need[i]].push(i);
}
vector<int> perm(sz);
for (int i = 0; i < sz; i++) {
int v = cur[i];
perm[i] = pos[v].front();
pos[v].pop();
}
// đếm cycle
vector<bool> visited(sz, false);
for (int i = 0; i < sz; i++) {
if (visited[i]) continue;
int cycle = 0;
int j = i;
while (!visited[j]) {
visited[j] = true;
j = perm[j];
cycle++;
}
if (cycle > 1)
ans += cycle - 1;
}
}
cout << ans;
}