結果

問題 No.837 Noelちゃんと星々2
ユーザー tomarint2tomarint2
提出日時 2019-06-14 21:55:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,396 bytes
コンパイル時間 958 ms
コンパイル使用メモリ 85,196 KB
実行使用メモリ 18,624 KB
最終ジャッジ日時 2024-04-26 15:45:01
合計ジャッジ時間 7,506 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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



void solve()
{
    ll N;
    cin >> N;
    vector<ll> Y(N);
    for1(i,N) cin >> Y[i];
    sort(Y.begin(),Y.end());
    if (Y[0] == Y[N-1]) {
        cout << 1 << endl;
        return;
    }
    vector<ll> csum(N+1,0);
    for1(i,N) csum[i+1] = csum[i] + Y[i];

    const ll inf = 1LL << 60;
    ll ans = inf;
    // [0,i) [i,N)
    for2(i,1,N) {
        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);
        }
        //cerr << i << " " << cost << endl;
        ans = min(ans, cost);
    }

    cout << ans << endl;
}

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