結果
| 問題 | No.366 ロボットソート |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-02-10 17:45:12 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 2,000 ms |
| コード長 | 1,231 bytes |
| 記録 | |
| コンパイル時間 | 2,195 ms |
| コンパイル使用メモリ | 224,948 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2026-02-10 17:45:16 |
| 合計ジャッジ時間 | 3,569 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 23 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct thue
{
ll c, t, p;
};
struct node
{
ll nt;
vector<ll> ds;
};
ll nghichthe = 0;
ll bbs(vector<ll> &list)
{
ll count = 0;
for(ll i=0;i<list.size()-1;i++)
{
bool sw = false;
for(ll j=0;j<list.size()-i-1;j++)
{
if(list[j] > list[j+1])
{
swap(list[j], list[j+1]);
count++;
sw = true;
}
}
if(!sw) break;
}
return count;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll n, k;
cin >> n >> k;
vector<ll> a(n);
for(ll i=0;i<n;i++) cin >> a[i];
unordered_map<ll, vector<ll>> cnt;
ll ans = 0;
for(ll i=0;i<n;i++)
{
cnt[(i+1)%k].push_back(a[i]);
}
sort(a.begin(), a.end());
for(auto &d : cnt)
{
ans+=bbs(d.second);
ll id = d.first;
if(id==0) id = k;
for(ll i=0;i<d.second.size();i++)
{
if(d.second[i] != a[id-1])
{
cout << -1;
return 0;
}
id+=k;
}
}
cout << ans;
return 0;
}