結果

問題 No.837 Noelちゃんと星々2
ユーザー mamekinmamekin
提出日時 2019-06-14 21:46:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,585 bytes
コンパイル時間 1,268 ms
コンパイル使用メモリ 120,672 KB
実行使用メモリ 4,880 KB
最終ジャッジ日時 2023-09-02 07:19:42
合計ジャッジ時間 2,930 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

template <class T>
class cumulativeSum
{
private:
    int n;
    vector<T> sum;
public:
    cumulativeSum(const vector<T>& a)
    {
        n = a.size();
        sum.assign(n+1, 0);
        for(int i=0; i<n; ++i)
            sum[i+1] = a[i] + sum[i];
    }
    T getSum(int x1, int x2)
    {
        x1 = max(x1, 0);
        x2 = min(x2, n);
        if(x1 >= x2)
            return 0;
        return sum[x2] - sum[x1];
    }
};

long long solve(const vector<long long>& v)
{
    if(v.front() == v.back())
        return 1;

    int n = v.size();
    cumulativeSum<long long> cs(v);
    long long ans = LLONG_MAX;
    for(int i=1; i<n; ++i){
        int a = i;
        int b = n - i;
        long long tmp1 = - cs.getSum(0, a/2) + cs.getSum((a+1)/2, a);
        long long tmp2 = - cs.getSum(a, a+b/2) + cs.getSum(a+(b+1)/2, a+b);
        ans = min(ans, tmp1 + tmp2);
    }
    return ans;
}

int main()
{
    int n;
    cin >> n;
    vector<long long> v(n);
    for(int i=0; i<n; ++i)
        cin >> v[i];
    sort(v.begin(), v.end());

    cout << solve(v) << endl;

    return 0;
}
0