結果

問題 No.693 square1001 and Permutation 2
ユーザー kpinkcatkpinkcat
提出日時 2023-08-19 17:44:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 844 bytes
コンパイル時間 1,874 ms
コンパイル使用メモリ 95,380 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 17:44:25
合計ジャッジ時間 1,993 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<map>
#include<vector>
#include <algorithm>
#include<math.h>
using namespace std;

int main()
{
    int n, cnt = 0;
    cin >> n;
    vector<int> v(n, 0);
    for (int i = 0; i < n; i++){
        int t;
        cin >> t;
        v[t-1]++;
    }
    for (int i = 0; i < n; i++){
        if (v[i] > 1){
            int j = i + 1;
            while (v[i] != 1){
                if (v[j] == 0){
                    cnt += (j - i);
                    v[i]--;
                    v[j]++;
                } else j++;
            }
        } else if (!v[i]){
            for (int j = i; j < n; j++){
                if (v[j] > 1){
                    cnt += (j - i);
                    v[j]--;
                    v[i]++;
                    break;
                }
            }
        }
    }
    cout << cnt << endl;
}
0