結果

問題 No.837 Noelちゃんと星々2
ユーザー rpy3cpprpy3cpp
提出日時 2019-09-08 14:41:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,385 bytes
コンパイル時間 1,527 ms
コンパイル使用メモリ 171,288 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-02 07:28:48
合計ジャッジ時間 3,212 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long diffsum(const vector<long long> & Ys, int head, int tail){
    long long result = 0LL;
    while (head < tail){
        result += Ys[tail--] - Ys[head++];
    }
    return result;
}


long long solve(int N, const vector<long long> & Ys){
    int a_head = 0;
    int a_tail = 0;
    int b_head = 1;
    int b_tail = N - 1;
    int a_mid = (a_head + a_tail) / 2;
    int b_mid = (b_head + b_tail) / 2;
    long long score = diffsum(Ys, a_head, a_tail) + diffsum(Ys, b_head, b_tail);
    long long record = score;
    for (a_tail = 1, b_head = 2; b_head < N; b_head++, a_tail++){
        a_mid = (a_head + a_tail) / 2;
        score += Ys[a_tail] - Ys[a_mid];
        b_mid = (b_head + b_tail) / 2;
        score -= Ys[b_mid] - Ys[b_head - 1];
        record = min(record, score);
    }
    return record;
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vector<long long> Ys(N, 0LL);
    for (auto & y: Ys) cin >> y;
    sort(Ys.begin(), Ys.end());
    // Corner case
    if (Ys[0] == Ys[N - 1]){
        cout << 1 << endl;
        return 0;
    }
    if (N == 2){
        cout << 0 << endl;
        return 0;
    }else if (N == 3){
        cout << min(Ys[2] - Ys[1], Ys[1] - Ys[0]) << endl;
        return 0;
    }
    // Regular case
    cout << solve(N, Ys) << endl;
    return 0;
}
0