結果

問題 No.837 Noelちゃんと星々2
ユーザー tomarint2tomarint2
提出日時 2019-06-14 23:08:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,332 bytes
コンパイル時間 894 ms
コンパイル使用メモリ 82,664 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-26 21:49:00
合計ジャッジ時間 2,775 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
5,248 KB
testcase_01 AC 61 ms
5,376 KB
testcase_02 AC 61 ms
5,376 KB
testcase_03 AC 61 ms
5,376 KB
testcase_04 AC 60 ms
5,376 KB
testcase_05 AC 60 ms
5,376 KB
testcase_06 AC 60 ms
5,376 KB
testcase_07 AC 60 ms
5,376 KB
testcase_08 AC 60 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 WA -
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 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;
    // 左側を合わせるコスト
    if (i % 2 == 1 || i < 2) {
        ll pivot = Y[i/2];
        for2(j,0,i) {
            cost += abs(Y[j] - pivot);
        }
    } else {
        ll pivot1 = Y[i/2-1];
        ll pivot2 = Y[i/2];
        ll cost1 = 0;
        ll cost2 = 0;
        for2(j,0,i) {
            cost1 += abs(Y[j] - pivot1);
            cost2 += abs(Y[j] - pivot2);
        }
        cost += min(cost1,cost2);
    }
    // 右側を合わせるコスト
    if ((N-i) % 2 == 1 || (N-i) < 2) {
        ll pivot = Y[(N-i)/2+i];
        for2(j,i,N) {
            cost += abs(Y[j] - pivot);
        }
    } else {
        ll pivot1 = Y[(N-i)/2+i-1];
        ll pivot2 = Y[(N-i)/2+i];
        ll cost1 = 0;
        ll cost2 = 0;
        for2(j,i,N) {
            cost1 += abs(Y[j] - pivot1);
            cost2 += abs(Y[j] - pivot2);
        }
        cost += min(cost1,cost2);
    }
    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 {
            r = p2;
        }
        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