結果

問題 No.693 square1001 and Permutation 2
ユーザー GBGB
提出日時 2018-09-24 16:16:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,019 bytes
コンパイル時間 630 ms
コンパイル使用メモリ 73,084 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 04:21:17
合計ジャッジ時間 1,255 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main() {

	int n;
	cin >> n;
	vector<int> array;
	array.resize(n + 1, 0);
	for (int i = 0;i<n;i++) {
		int num;
		cin >> num;
		array[num]++;
	}

	int cost = 0;
	bool flag = false;

	while (!flag) {
		int target1 = 0, target2 = 0;
		for (int i = 1;i<array.size();i++) {
			if (array[i] == 0) {
				target1 = i;
				break;
			}
		}
		int distance = 10000;
		for (int i = 1;i < array.size();i++) {
			if (array[i]>1) {
				distance = min(distance,abs(target1-i));
				if (distance == abs(target1 - i)) {
					target2 = i;
				}
			}
		}
		if (target1==0 && target2) {
			break;
		}
		else if (target2 > target1) {
			cost += target2 - target1;
			array[target2]--;
			array[target1]++;
		}
		else if (target2 < target1) {
			cost += target1 - target2;
			array[target2]--;
			array[target1]++;
		}
		for (int i = 1;i<array.size();i++) {
			if (array[i] == 1)flag = true;
			else flag = false;
		}
	}

	cout << cost << endl;
	
	return 0;
}
0