Submission #5370264


Source Code Expand

#include<bits/stdc++.h>
using namespace std;

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const long long MOD = 1000000007LL;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

int n;
vector<lint> a;
int memo[2][1001];

template<typename T>
bool chmax(T &_a, T _b){
    if(_a < _b){
        _a = _b;
        return true;
    }else{
        return false;
    }
}

template<typename T>
bool chmin(T &_a, T _b){
    if(_a > _b){
        _a = _b;
        return true;
    }else{
        return false;
    }
}

int solve(int t, int k, const lint mid){
    if(k == n){
        return 0;
    }else if(memo[t][k] >= -2000){
        return memo[t][k];
    }

    int res;
    lint val = 0;
    if(t == 0){
        res = -2000;
        REP(i, k, n){
            val += a[i];
            if(val >= mid){
                chmax(res, solve(1, i + 1, mid) + 1);
            }else{
                chmax(res, solve(1, i + 1, mid) - 1);
            }
        }
    }else{
        res = 2000;
        REP(i, k, n){
            val += a[i];
            if(val >= mid){
                chmin(res, solve(0, i + 1, mid) + 1);
            }else{
                chmin(res, solve(0, i + 1, mid) - 1);
            }
        }
    }

    return memo[t][k] = res;
}

bool check(long long mid){
    fill(memo[0], memo[2], -3000);
    return solve(0, 0, mid) >= 0;
}

long long meguru_search(long long ok, long long ng){
    while(abs(ok - ng) > 1LL){
        long long mid = (ok + ng) / 2LL;
        if(check(mid)){
            ok = mid;
        }else{
            ng = mid;
        }
    }
    return ok;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n;

    a.resize(n);
    rep(i, n){
        cin >> a[i];
    }

    lint ans = meguru_search(-1e13, 1e13);
    cout << ans << endl;

    return 0;
}

Submission Info

Submission Time
Task H - Median Game
User mhrb
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2577 Byte
Status AC
Exec Time 200 ms
Memory 384 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 32
Set Name Test Cases
Sample s1.txt, s2.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, s1.txt, s2.txt
Case Name Status Exec Time Memory
01.txt AC 1 ms 256 KB
02.txt AC 1 ms 256 KB
03.txt AC 1 ms 256 KB
04.txt AC 1 ms 256 KB
05.txt AC 1 ms 256 KB
06.txt AC 1 ms 256 KB
07.txt AC 1 ms 256 KB
08.txt AC 1 ms 256 KB
09.txt AC 1 ms 256 KB
10.txt AC 1 ms 256 KB
11.txt AC 189 ms 384 KB
12.txt AC 184 ms 384 KB
13.txt AC 159 ms 384 KB
14.txt AC 178 ms 384 KB
15.txt AC 169 ms 384 KB
16.txt AC 184 ms 384 KB
17.txt AC 188 ms 384 KB
18.txt AC 174 ms 384 KB
19.txt AC 184 ms 384 KB
20.txt AC 187 ms 384 KB
21.txt AC 177 ms 384 KB
22.txt AC 182 ms 384 KB
23.txt AC 184 ms 384 KB
24.txt AC 176 ms 384 KB
25.txt AC 189 ms 384 KB
26.txt AC 186 ms 384 KB
27.txt AC 186 ms 384 KB
28.txt AC 200 ms 384 KB
29.txt AC 181 ms 384 KB
30.txt AC 189 ms 384 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB