結果
| 問題 | No.366 ロボットソート | 
| コンテスト | |
| ユーザー |  kotamanegi | 
| 提出日時 | 2016-04-29 22:39:13 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3 ms / 2,000 ms | 
| コード長 | 1,417 bytes | 
| コンパイル時間 | 984 ms | 
| コンパイル使用メモリ | 86,456 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-12-29 17:39:16 | 
| 合計ジャッジ時間 | 1,904 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 23 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:30:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |         scanf("%d%d", &n, &k);
      |         ~~~~~^~~~~~~~~~~~~~~~
main.cpp:31:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   31 |         REP(i, n) scanf("%d", &a[i]);
      |                   ~~~~~^~~~~~~~~~~~~
            
            ソースコード
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#define  _SCL_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream> 
#include<map>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cstdint>
#define REP(i,n) for(int i = 0;i < n;++i)
using namespace std;
int a[2000] = {};
int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	REP(i, n) scanf("%d", &a[i]);
	vector<int> hoge;
	REP(i, n) hoge.push_back(a[i]);
	sort(begin(hoge), end(hoge));
	int ans = 0;
	for (int i = 0;i < n;++i) {
		int where_isa = -1;
		for (int q = 0;q < n;++q) {
			if (hoge[i] == a[q]) {
				where_isa = q;
				goto next;
			}
		}
	next:;
		if (where_isa != i) {
			if (where_isa > i) {
				while (true) {
					ans++;
					where_isa -= k;
					if (where_isa < 0 || where_isa < i) {
						goto cant;
					}
					swap(a[where_isa], a[where_isa + k]);
					if (where_isa == i) break;
				}
			}else{
				while (true) {
					ans++;
					where_isa += k;
					if (where_isa >= n) {
						goto cant;
					}
					swap(a[where_isa], a[where_isa - k]);
					if (where_isa == i) break;
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
cant:;
	cout << "-1" << endl;
	return 0;
}
            
            
            
        