Submission #708524

#TimeUsernameProblemLanguageResultExecution timeMemory
708524jophyyjhThe Big Prize (IOI17_prize)C++14
97.68 / 100
51 ms336 KiB
/**
 * It's not that hard to come up with a almost-full solution. First, let non-lollipop
 * be any prize with val < v. The number of non-lollipops is O(sqrt(n)), and there're
 * at most 5 types of prizes.
 * 
 * One can also notice that if two prizes u, v have the same value, then the sum of
 * reponse for u and v is the same. Therefore, query any O(sqrt(n)) positions and we
 * will find the number of non-lollipops. Finally, we know that lollipops account for
 * most of the prizes, and their "left response" is increasing from left to right. We
 * can treat (a[0]) from lollipops as an increasing sequence, and apply binary search
 * to find all non-lollipops. Naturally, we will find the diamond.
 * Doing O(sqrt(n)) separate binary searches require about 7000-9000 steps. In impl1,
 * I optimize my search by applying those binary searches simultaneously. Namely, for
 * each range [l,r] we're working with, query pos (l+r)/2 first. The monotonicity
 * helps us determine the range of values for [l,(l+r)/2) and ((l+r)/2,r]. The
 * approach is analogous to the well-known divide-and-conquer optimization. I know
 * that this method has the same complexity, yet the constant, though I don't know,
 * is smaller.
 * 
 * I scored 95.56 points with impl1, with the max query count being 5444. This means
 * we're quite close~ Indeed, "parallel binary search" takes less queries than 
 * O(sqrt(n)) instances of binary search. In impl1.5, I lowered the num of searches
 * to find a lollipop: it turns out that my computation was incorrect (yielding a
 * suboptimal bound). By using a python script, I brute-forced the max num of
 * non-lollipops - 472, achieved with (1, 4, 21, 446, 199528).
 * 
 * Queries needed:  sqrt(n) * log(n)    (unsure about the const)
 * Implementation 1.5
*/

#include <bits/stdc++.h>
#include "prize.h"

typedef std::vector<int>    vec;

const int MAX_TYPES = 5;
const int MAX_LOL = 472;    // max num of non-lollipops


int non_lol;    // number of non-lollipops

int search(int l, int r, int val_lower, int val_upper) {
    if (l > r || val_lower == val_upper)
        return -1;
    int mid = (l + r) / 2, pt = mid, t = -1;
    for (; l <= pt; pt--) {
        vec res = ask(pt);
        if (res[0] + res[1] == 0)   // diamond found
            return pt;
        if (res[0] + res[1] == non_lol) {
            t = res[0];
            break;
        }
    }
    return std::max(search(l, pt - 1, val_lower, t),
                        search(mid + 1, r, t + (mid - pt), val_upper));
}

int find_best(int n) {
    non_lol = 0;
    std::set<int> distinct_sum;
    for (int k = 0; k <= MAX_LOL; k++) {
        int pos = std::min(n / MAX_LOL * k, n - 1);     // roughly evenly-spaced
        vec res = ask(pos);
        int s = res[0] + res[1];
        if (s == 0)
            return pos;
        non_lol = std::max(non_lol, s);
        distinct_sum.emplace(s);
        if (int(distinct_sum.size()) >= MAX_TYPES)
            break;
    }
    return search(0, n - 1, 0, MAX_LOL);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...