結果

問題 No.837 Noelちゃんと星々2
ユーザー hipopohipopo
提出日時 2020-01-21 15:11:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,525 bytes
コンパイル時間 1,089 ms
コンパイル使用メモリ 107,616 KB
実行使用メモリ 4,688 KB
最終ジャッジ日時 2023-09-19 05:32:31
合計ジャッジ時間 4,570 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
4,388 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 74 ms
4,544 KB
testcase_04 AC 74 ms
4,380 KB
testcase_05 AC 74 ms
4,432 KB
testcase_06 AC 74 ms
4,392 KB
testcase_07 AC 74 ms
4,552 KB
testcase_08 AC 75 ms
4,492 KB
testcase_09 AC 3 ms
4,544 KB
testcase_10 AC 3 ms
4,560 KB
testcase_11 AC 3 ms
4,388 KB
testcase_12 WA -
testcase_13 AC 3 ms
4,492 KB
testcase_14 AC 3 ms
4,392 KB
testcase_15 AC 3 ms
4,552 KB
testcase_16 AC 3 ms
4,536 KB
testcase_17 AC 3 ms
4,504 KB
testcase_18 AC 3 ms
4,688 KB
testcase_19 AC 3 ms
4,688 KB
testcase_20 AC 3 ms
4,396 KB
testcase_21 AC 3 ms
4,616 KB
testcase_22 AC 3 ms
4,556 KB
testcase_23 AC 3 ms
4,500 KB
testcase_24 AC 3 ms
4,600 KB
testcase_25 AC 3 ms
4,392 KB
testcase_26 AC 3 ms
4,544 KB
testcase_27 AC 3 ms
4,436 KB
testcase_28 AC 3 ms
4,688 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 3 ms
4,520 KB
testcase_31 AC 3 ms
4,520 KB
testcase_32 AC 3 ms
4,540 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; }

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