結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
5,248 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 54 ms
5,376 KB
testcase_04 AC 55 ms
5,376 KB
testcase_05 AC 55 ms
5,376 KB
testcase_06 AC 55 ms
5,376 KB
testcase_07 AC 53 ms
5,376 KB
testcase_08 AC 54 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 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 1 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;

// [0,i) [i,N)
ll calc(ll i) {
    ll cost = 0;
    // 左側を合わせるコスト
    ll pivot = Y[i/2];
    for2(j,0,i) {
        cost += abs(Y[j] - pivot);
    }
    // 右側を合わせるコスト
    pivot = Y[(N-i)/2+i];
    for2(j,i,N) {
        cost += abs(Y[j] - pivot);
    }
    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 (p1 < p2-1) {
        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 = 1LL << 60;
    for2(i,l,r) {
        ans = min(ans, calc(i));
    }
    cout << ans << endl;
}

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