Submission #3665441


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int M = 1000000007;


int n;
vector<long long> a;
vector<vector<int>> memo;

int dfs(int ai, int p, long long th) {
    if (ai == n) return 0;
    if (memo[ai][p] != -M) return memo[ai][p];
    int ret;
    long long sum = 0;
    if (p == 0) {
        ret = -M;
        for (int i = ai; i < n; ++i) {
            sum += a[i];
            ret = max(ret, dfs(i + 1, !p, th) + (sum >= th ? 1 : -1));
        }
    }
    else {
        ret = M;
        for (int i = ai; i < n; ++i) {
            sum += a[i];
            ret = min(ret, dfs(i + 1, !p, th) + (sum >= th ? 1 : -1));
        }
    }
    return memo[ai][p] = ret;
}


int main() {
    cin >> n;
    a = vector<long long>(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    long long ok = -1e13, ng = 1e13;
    while (ok < ng - 1) {
        long long m = (ok + ng) / 2;
        memo = vector<vector<int>>(n, vector<int>(2, -M));
        if (dfs(0, 0, m) >= 0) {
            ok = m;
        }
        else {
            ng = m;
        }
    }
    cout << ok << "\n";
    return 0;
}

Submission Info

Submission Time
Task H - Median Game
User riantkb
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1134 Byte
Status AC
Exec Time 215 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 212 ms 384 KB
12.txt AC 200 ms 384 KB
13.txt AC 177 ms 384 KB
14.txt AC 200 ms 384 KB
15.txt AC 184 ms 384 KB
16.txt AC 202 ms 384 KB
17.txt AC 208 ms 384 KB
18.txt AC 192 ms 384 KB
19.txt AC 203 ms 384 KB
20.txt AC 203 ms 384 KB
21.txt AC 193 ms 384 KB
22.txt AC 199 ms 384 KB
23.txt AC 198 ms 384 KB
24.txt AC 196 ms 384 KB
25.txt AC 206 ms 384 KB
26.txt AC 196 ms 384 KB
27.txt AC 194 ms 384 KB
28.txt AC 215 ms 384 KB
29.txt AC 197 ms 384 KB
30.txt AC 205 ms 384 KB
s1.txt AC 1 ms 256 KB
s2.txt AC 1 ms 256 KB