結果
問題 |
No.366 ロボットソート
|
ユーザー |
![]() |
提出日時 | 2016-04-29 23:16:32 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,452 bytes |
コンパイル時間 | 685 ms |
コンパイル使用メモリ | 85,468 KB |
実行使用メモリ | 13,632 KB |
最終ジャッジ日時 | 2024-10-04 18:52:39 |
合計ジャッジ時間 | 4,295 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 TLE * 1 -- * 6 |
ソースコード
#include <algorithm> #include <numeric> #include <string> #include <vector> #include <iostream> #include <queue> #include <utility> #include <cmath> #include <bitset> using namespace std; bool check(int index1,int index2,int k) { int sub = abs(index1 - index2); if(sub % k == 0) return true; else return false; } int main() { int n,k; cin >> n >> k; vector<int> a(n); for(auto&& x : a) { cin >> x; } vector<int> sorted_a = a; sort(sorted_a.begin(),sorted_a.end()); if(k > n) { if(sorted_a == a) { cout << 0 << endl; return 0; } else { cout << -1 << endl; return 0; } } int count = 0; int i = 0; for(int i =0; i < n; ++i) { auto iter1 = find(a.begin(), a.end(),a.at(i)); int index1 = distance(a.begin(), iter1); auto iter2 = find(sorted_a.begin(), sorted_a.end(),a.at(i)); int index2 = distance(sorted_a.begin(), iter2); if(!check(index1,index2,k)) { cout << -1 << endl; return 0; } } while(a != sorted_a) { if(sorted_a.at(i) != a.at(i)) { if(i+k < n) { swap(a.at(i),a.at(i+k)); ++count; } } ++i; if(i == n) i = 0; } cout << count << endl; }