Submission #501485

# Submission time Handle Problem Language Result Execution time Memory
501485 2022-01-03T12:55:36 Z Stickfish Gap (APIO16_gap) C++17
100 / 100
46 ms 3220 KB
#include "gap.h"
#include <vector>
#include <algorithm>
#include <random>
using ll = long long;
using namespace std;

mt19937 rng(time(0));
const ll INF = 1.77013e18;

ll findGap(int T, int N) {
    if (T == 1) {
        vector<ll> mn;
        vector<ll> mx;
        while (mn.size() + mx.size() < N) {
            ll nmn, nmx;
            if (mn.empty()) {
                MinMax(1ll, INF, &nmn, &nmx);
            } else {
                MinMax(mn.back() + 1, mx.back() - 1, &nmn, &nmx);
            }
            mn.push_back(nmn);
            if (nmn != nmx)
                mx.push_back(nmx);
        }
        reverse(mx.begin(), mx.end());
        for (auto x : mx) {
            mn.push_back(x);
        }
        ll ans = 0;
        for (int i = 0; i + 1 < N; ++i) {
             ans = max(ans, mn[i + 1] - mn[i]);
        }
        return ans;
    } else {
        ll mn, mx;
        MinMax(1, INF, &mn, &mx);
        ll ans = (mx - mn - 1) / (N - 1) + 1;
        ll mxans = mx - mn;
        ll curvl = mn;
        vector<pair<ll, ll>> gaps;
        while (ans < mxans && curvl < mx) {
            ll nmn, nmx;
            MinMax(curvl + 1, curvl + ans * 2 - 1, &nmn, &nmx);
            if (nmn != -1) {
                ans = max(ans, nmn - curvl);
                gaps.push_back({nmn, nmx});
                curvl = nmx;
            } else {
                ans *= 2;
            }
        }
        shuffle(gaps.begin(), gaps.end(), rng);
        for (auto [l, r] : gaps) {
            if (r - l > ans) {
                ll md = (l + r) / 2;
                ll lmn, lmx;
                MinMax(l + 1, md, &lmn, &lmx);
                if (lmx != -1 && r - lmx <= ans)
                    continue;
                ll rmn, rmx;
                MinMax(md + 1, r - 1, &rmn, &rmx);
                if (lmn == -1 && rmn == -1) {
                    ans = r - l;
                } else if (lmn == -1) {
                    ans = max(ans, rmn - l);
                } else if (rmn == -1) {
                    ans = max(ans, r - lmx);
                } else {
                    ans = max(ans, rmn - lmx);
                }
            }
        }
        return ans;
    }
    return 0;
}

Compilation message

gap.cpp: In function 'll findGap(int, int)':
gap.cpp:15:38: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   15 |         while (mn.size() + mx.size() < N) {
      |                ~~~~~~~~~~~~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 200 KB Output is correct
2 Correct 0 ms 200 KB Output is correct
3 Correct 0 ms 200 KB Output is correct
4 Correct 0 ms 200 KB Output is correct
5 Correct 0 ms 200 KB Output is correct
6 Correct 0 ms 200 KB Output is correct
7 Correct 1 ms 200 KB Output is correct
8 Correct 0 ms 200 KB Output is correct
9 Correct 0 ms 200 KB Output is correct
10 Correct 0 ms 200 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 328 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 10 ms 968 KB Output is correct
17 Correct 9 ms 968 KB Output is correct
18 Correct 10 ms 920 KB Output is correct
19 Correct 11 ms 968 KB Output is correct
20 Correct 7 ms 968 KB Output is correct
21 Correct 41 ms 2616 KB Output is correct
22 Correct 37 ms 2604 KB Output is correct
23 Correct 38 ms 2572 KB Output is correct
24 Correct 37 ms 2604 KB Output is correct
25 Correct 35 ms 2532 KB Output is correct
26 Correct 34 ms 2588 KB Output is correct
27 Correct 36 ms 2548 KB Output is correct
28 Correct 46 ms 2612 KB Output is correct
29 Correct 36 ms 2620 KB Output is correct
30 Correct 28 ms 2536 KB Output is correct
31 Correct 0 ms 200 KB Output is correct
32 Correct 0 ms 200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 0 ms 200 KB Output is correct
3 Correct 0 ms 200 KB Output is correct
4 Correct 0 ms 200 KB Output is correct
5 Correct 0 ms 200 KB Output is correct
6 Correct 0 ms 200 KB Output is correct
7 Correct 0 ms 200 KB Output is correct
8 Correct 0 ms 200 KB Output is correct
9 Correct 0 ms 200 KB Output is correct
10 Correct 0 ms 200 KB Output is correct
11 Correct 1 ms 328 KB Output is correct
12 Correct 1 ms 328 KB Output is correct
13 Correct 1 ms 328 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 328 KB Output is correct
16 Correct 6 ms 456 KB Output is correct
17 Correct 6 ms 456 KB Output is correct
18 Correct 6 ms 564 KB Output is correct
19 Correct 6 ms 548 KB Output is correct
20 Correct 3 ms 456 KB Output is correct
21 Correct 23 ms 1276 KB Output is correct
22 Correct 23 ms 1216 KB Output is correct
23 Correct 26 ms 1260 KB Output is correct
24 Correct 25 ms 1196 KB Output is correct
25 Correct 46 ms 3220 KB Output is correct
26 Correct 22 ms 1220 KB Output is correct
27 Correct 24 ms 1292 KB Output is correct
28 Correct 24 ms 1292 KB Output is correct
29 Correct 30 ms 1216 KB Output is correct
30 Correct 11 ms 1152 KB Output is correct
31 Correct 0 ms 200 KB Output is correct
32 Correct 0 ms 200 KB Output is correct