結果

問題 No.693 square1001 and Permutation 2
ユーザー GBGB
提出日時 2018-09-24 17:58:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 995 bytes
コンパイル時間 1,481 ms
コンパイル使用メモリ 74,240 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-23 19:30:04
合計ジャッジ時間 2,234 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 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 1 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 = n == 1 ? 0 : 10000;

	vector<int> target1;
	vector<int> target2;
	for (int i = 1;i < array.size();i++) {
		if (array[i]==0) {
			target2.push_back(i);
		}
		else if (array[i]>1) {
			for (int j = 0;j < array[i]-1;j++) {
				target1.push_back(i);
			}
		}
	}
	if (target2.size() == 0) cost = 0;
	else {
		for (int i = 0;i < target1.size();i++) {
			vector<int> buf;
			copy(target1.begin(), target1.end(), back_inserter(buf));
			int calc = 0;
			for (int j = 0;j < target1.size();j++) {
				calc += abs(target2[j] - target1[j]);
			}
			cost = min(cost, calc);
			for (int j = 0;j < target1.size();j++) {
				if (j == 0)target1[j] = buf[target1.size() - 1];
				else {
					target1[j] = buf[j - 1];
				}
			}
		}
	}

	cout << cost<<endl;

	
	return 0;
}
0