結果

問題 No.837 Noelちゃんと星々2
ユーザー yuliicppyyuliicppy
提出日時 2019-06-14 22:35:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,407 bytes
コンパイル時間 911 ms
コンパイル使用メモリ 92,460 KB
実行使用メモリ 6,328 KB
最終ジャッジ日時 2023-09-02 07:21:11
合計ジャッジ時間 2,353 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
6,208 KB
testcase_01 AC 25 ms
6,160 KB
testcase_02 AC 25 ms
6,272 KB
testcase_03 AC 25 ms
6,260 KB
testcase_04 AC 25 ms
6,328 KB
testcase_05 AC 25 ms
6,272 KB
testcase_06 AC 25 ms
6,192 KB
testcase_07 AC 25 ms
6,260 KB
testcase_08 AC 24 ms
6,148 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 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,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 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 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    VL Y(N); REP(i,N) cin >> Y[i];
    bool ng = 1;
    REP(i,N){
        if(Y[i] != Y[0])  ng= 0;
    }
    if(ng) {
        cout << 1 << endl;
        return 0;
    }

    sort(Y.begin(),Y.end());

    VL rY = Y;
    reverse(rY.begin(),rY.end());

    LL tmpa = 0;
    LL tmpb = 0;

    VL memo1(N+1,0);
    VL memo2(N+1,0);
    FOR(i,2,N+1){
        if(i % 2 == 1){
            tmpa += abs(Y[i/2]-Y[i-1]);
            //tmpa += abs(Y[i/2 - 1]-Y[i/2]) * (i/2 - 1);
        }
        else{
            tmpa += abs(Y[(i-1)/2]-Y[i-1]);
        }
        memo1[i] = tmpa;
    }
    FOR(i,2,N+1){
        if(i % 2 == 1){
            tmpb += abs(rY[i/2]-rY[i-1]);
            //tmpb += abs(rY[i/2 - 1]-rY[i/2]) * (i/2 - 1);
        }
        else{
            tmpb += abs(rY[(i-1)/2]-rY[i-1]);
        }
        memo2[i] = tmpb;
    }
    LL ans = LINF;

    //REP(i,N+1){
    //    dump(memo1[i])
    //}
    //REP(i,N+1){
    //    dump(memo2[i])
    //}

    REP(i,N+1){
        if(ans > memo1[i]+memo2[N-i]) ans = memo1[i] + memo2[N-i];
    }
    cout << ans << endl;


}
0