結果

問題 No.837 Noelちゃんと星々2
ユーザー tomarint2tomarint2
提出日時 2019-06-15 01:38:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,987 bytes
コンパイル時間 678 ms
コンパイル使用メモリ 81,436 KB
実行使用メモリ 4,412 KB
最終ジャッジ日時 2023-09-02 07:23:19
合計ジャッジ時間 2,613 ms
ジャッジサーバーID
(参考情報)
judge12 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstring>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <deque>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
#define for1(i,n) for (ll i=0;i<(n);i++)
#define for2(i,m,n) for (ll i=(m);i<(n);i++)
#define for3(i,m,n,d) for (ll i=(m);i<(n);i+=(d))
#define DEBUG 0
template <class T>
void dumplist(T list)
{
    for (auto item : list) {
        cout << item << " ";
    }
    cout << endl;
}

ll Y[100010];
ll N;
const ll inf = 1LL << 60;

// [0,i) [i,N)
ll calc(ll i) {
    if (i == 0) return inf;
    if (i == N) return inf;
    ll cost = 0;
    // 左側を合わせるコスト
    ll pivot = Y[i/2];
    for2(j,0,i) {
        cost += abs(Y[j] - pivot);
        //cout << Y[j] << " - " << pivot << " = " << abs(Y[j] - pivot) << endl;
    }
    // 右側を合わせるコスト
    pivot = Y[(N-i)/2+i];
    for2(j,i,N) {
        cost += abs(Y[j] - pivot);
        //cout << Y[j] << " - " << pivot << " = " << abs(Y[j] - pivot) << endl;
    }
    //cout << "Cost : " << i << " : " << cost << endl;
    return cost;
}

void solve()
{
    cin >> N;
    for1(i,N) cin >> Y[i];
    sort(&Y[0],&Y[N]);
    if (Y[0] == Y[N-1]) {
        cout << 1 << endl;
        return;
    }

    ll l = 0;
    ll r = N;
    ll p1 = (r-l)*1/3+l;
    ll p2 = (r-l)*2/3+l;
    while (l < r - 10) {
        ll a1 = calc(p1);
        ll a2 = calc(p2);
        if (a1 > a2) {
            l = p1;
        } else if (a1 < a2) {
            r = p2+1;
        } else {
            l = (l + p1) / 2;
            r = (r + (p2+1)) / 2;
        }
        p1 = (r-l)*1/3+l;
        p2 = (r-l)*2/3+l;
        //cerr << l << " " << p1 << " " << p2 << " " << r << endl;
    }

    ll ans = inf;
    for2(i,l,r) {
        ans = min(ans, calc(i));
    }
    cout << ans << endl;
}

int main()
{
    do {
        solve();
    } while (DEBUG);
}
0