結果

問題 No.366 ロボットソート
ユーザー hotpepsihotpepsi
提出日時 2016-04-30 23:38:25
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,027 bytes
コンパイル時間 650 ms
コンパイル使用メモリ 68,632 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 08:12:21
合計ジャッジ時間 3,212 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,948 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 2 ms
6,944 KB
testcase_22 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <cstdio>

using namespace std;

class BIT {
	std::vector<int> bit;
	int size;
public:
	BIT() { }
	BIT(int sz) { init(sz); }
	void init(int sz) {
		bit = std::vector<int>((size = sz) + 1);
	}
	int sum(int i) {
		int s = 0;
		while (i > 0) {
			s += bit[i];
			i -= i & -i;
		}
		return s;
	}
	void add(int i, int x) {
		while (i <= size) {
			bit[i] += x;
			i += i & -i;
		}
	}
};

int main(int argc, char *argv[]) {
	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());
	int ans = 0;
	if (a != b) {
		for (int i = 0; i < K; ++i) {
			BIT bit(1024);
			vector<int> c;
			for (int j = i; j < N; j += K) {
				ans += j / K - bit.sum(a[j]);
				bit.add(a[j], 1);
				c.push_back(a[j]);
			}
			sort(c.begin(), c.end());
			for (int j = i; j < N; j += K) {
				a[j] = c[j/K];
			}
		}
		if (a != b) {
			ans = -1;
		}
	}
	cout << ans << endl;
	return 0;
}
0