結果

問題 No.693 square1001 and Permutation 2
ユーザー GBGB
提出日時 2018-09-24 17:53:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 930 bytes
コンパイル時間 684 ms
コンパイル使用メモリ 74,144 KB
実行使用メモリ 4,368 KB
最終ジャッジ日時 2023-10-23 19:17:42
合計ジャッジ時間 1,372 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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);
			}
		}
	}
	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