結果
問題 | No.693 square1001 and Permutation 2 |
ユーザー |
|
提出日時 | 2018-09-24 17:58:50 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 995 bytes |
コンパイル時間 | 754 ms |
コンパイル使用メモリ | 74,448 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-22 13:13:33 |
合計ジャッジ時間 | 1,449 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 9 |
ソースコード
#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;}