結果

問題 No.838 Noelちゃんと星々3
ユーザー face4face4
提出日時 2019-11-18 10:15:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 738 bytes
コンパイル時間 1,776 ms
コンパイル使用メモリ 77,996 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-04-08 13:25:25
合計ジャッジ時間 2,359 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
6,676 KB
testcase_01 AC 54 ms
6,676 KB
testcase_02 AC 52 ms
6,676 KB
testcase_03 AC 52 ms
6,676 KB
testcase_04 AC 53 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> v(n);
    for(int i = 0; i < n; i++)  cin >> v[i];
    sort(v.begin(), v.end());
    int ans = 0;
    if(n <= 3){
        ans = v.back()-v[0];
    }else{
        int INF = 2e9+1;
        vector<vector<int>> dp(2, vector<int>(n, INF));
        dp[0][1] = v[1]-v[0];
        dp[1][2] = v[2]-v[0];
        dp[0][3] = dp[0][1] + v[3]-v[2];
        for(int i = 4; i < n; i++){
            dp[0][i] = v[i]-v[i-1] + min(dp[0][i-2], dp[1][i-2]);
            dp[1][i] = v[i]-v[i-2] + min(dp[0][i-3], dp[1][i-3]);
        }
        ans = min(dp[0].back(), dp[1].back());
    }
    cout << ans << endl;
    return 0;
}
0