結果

問題 No.837 Noelちゃんと星々2
ユーザー hipopohipopo
提出日時 2020-01-22 15:20:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,219 bytes
コンパイル時間 1,075 ms
コンパイル使用メモリ 106,680 KB
実行使用メモリ 4,700 KB
最終ジャッジ日時 2023-09-02 07:29:44
合計ジャッジ時間 2,953 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
4,624 KB
testcase_01 AC 49 ms
4,636 KB
testcase_02 AC 49 ms
4,640 KB
testcase_03 AC 49 ms
4,636 KB
testcase_04 AC 49 ms
4,628 KB
testcase_05 AC 48 ms
4,620 KB
testcase_06 AC 48 ms
4,700 KB
testcase_07 AC 50 ms
4,548 KB
testcase_08 AC 49 ms
4,688 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 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,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<int, int>;

const long long MOD = 1e9+7;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() { 
    int n;
    cin >> n;
    vector<ll> y(n);
    for (int i = 0; i < n; i++) {
        cin >> y.at(i);
    }    
    sort(y.begin(), y.end());
    
    if (y.at(0) == y.at(n - 1)) {
        cout << 1 << endl;
        return 0;
    }
    
    vector<ll> sum(n + 1);
    for (int i = 0; i < n; i++) {
        sum.at(i + 1) = sum.at(i) + y.at(i);
    }
    //[l, r)
    auto range_sum = [&](int l, int r){ return sum.at(r) - sum.at(l); };

    auto sum_dist = [&](int l, int r){
        int m = (l + r) / 2;
        ll res = y.at(m) * (m - l) - range_sum(l, m) - y.at(m) * (r - m) + range_sum(m, r);
        return res;
    };
    
    ll mini = 1e18;
    for (int p = 1; p < n; p++) {
        chmin(mini, sum_dist(0, p) + sum_dist(p, n));
    }
    cout << mini << endl;
}   
0