結果

問題 No.837 Noelちゃんと星々2
ユーザー hipopo
提出日時 2020-01-21 15:11:44
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,525 bytes
コンパイル時間 1,368 ms
コンパイル使用メモリ 104,200 KB
最終ジャッジ日時 2025-01-08 20:23:50
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26 WA * 3
権限があれば一括ダウンロードができます

ソースコード

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; }

ll ternary_search(int l, int r, ll f(int p)) {
    ll res = 1e18;
    for (int _l = 0; _l < 100; _l++) {
        int c = (l * 2 + r) / 3, d = (l + r * 2) / 3;
        //cout << c << " " << d << endl;
        ll fc = f(c), fd = f(d);
        chmin(res, min(fc, fd));
        if (fc > fd) l = c;
        else r = d;
    }
    return res;
}

vector<ll> y(200000);
int n;

//y0  y1  y2  y3...
//  p1  p2  p3 ...
ll f(int p) {
    ll sum = 0;
    for (int i = 0; i < p; i++) {
        sum += abs(y.at(i) - y.at(p / 2));
        //sum += abs(y.at(i) - (y.at(0) + y.at(p - 1)) / 2);
    }
    for (int i = p; i < n; i++) {
        sum += abs(y.at(i) - y.at(p + (n - p) / 2));
        //sum += abs(y.at(i) - (y.at(p) + y.at(n - 1)) / 2);
    }
    return sum;
}

int main() { 
    cin >> n;
    y.resize(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;
    }
    
    //for (int i = 1; i < n; i++) cout << f(i) << endl;
    cout << ternary_search(0, n, f) << endl;
}   
0