結果
問題 | No.366 ロボットソート |
ユーザー |
![]() |
提出日時 | 2016-04-29 22:45:16 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 4 ms / 2,000 ms |
コード長 | 977 bytes |
コンパイル時間 | 870 ms |
コンパイル使用メモリ | 75,444 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-12-29 17:40:10 |
合計ジャッジ時間 | 1,651 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
#include <algorithm>#include <cstdio>#include <iostream>#include <map>#include <math.h>#include <queue>#include <set>#include <sstream>#include <stack>#include <string>#include <vector>using namespace std;#define ll long long#define INF (1 << 30)#define INFLL (1LL << 60)int n,a[1010] = {},ans = 0,b[1010],c;int main() {cin >> n >> c;for(int i = 0;i < n;i++){cin >> a[i];b[i] = a[i];}sort(b,b + n);bool flag = false;for(int now = 0;now < n;now++){for(int i = 0;i < n;i++){if(a[i] == b[now]){int p = i;while(a[p] != b[p]){if(a[p] < b[p]){if(p - c < 0 || b[p - c] < a[p]) flag = true;swap(a[p],a[p - c]);p -= c;}else if(a[p] > b[p]){if(p + c >= n || b[p + c] > a[p]) flag = true;swap(a[p],b[p + c]);p += c;}if(flag) break;ans++;}}if(flag) break;}if(flag) break;}if(flag) cout << -1 << endl;else cout << ans << endl;return 0;}