#include "prize.h"
#include <algorithm>
#include <iostream>
#include <chrono>
#include <random>
#include <vector>
using namespace std;
#ifdef MANSON
mt19937 rng(305);
#else
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif // MANSON
namespace
{
const int maxN = 200000;
int n;
vector<int> cache[maxN];
inline vector<int> Ask(int i)
{
if (cache[i].size()) return cache[i];
return cache[i] = ask(i);
}
int mx = 0;
inline bool Cheapest(int i)
{
vector<int> res = Ask(i);
bool f = (res[0] + res[1] == mx);
// cerr << i << " is" << (f ? " " : "n't ") << " a cheapy.\n";
return f;
}
int Solve(int l, int r)
{
uniform_int_distribution<int> rnd(1, 2);
if (rnd(rng) == 1)
{
while (l <= r && !Cheapest(l))
{
vector<int> res = Ask(l);
if (res[0] + res[1] == 0) return l;
++l;
}
int len = 1;
for (int x = (1 << 17); x >= 1; x >>= 1)
{
int R = l + (len + x) - 1;
if (R > r) continue;
vector<int> res1 = Ask(l), res2 = Ask(R);
if (res1[1] == res2[1])
len += x;
}
if (l > r) return -1;
if (l + len <= r)
return Solve(l + len, r);
return -1;
}
while (l <= r && !Cheapest(r))
{
vector<int> res = Ask(r);
if (res[0] + res[1] == 0) return r;
--r;
}
if (l > r) return -1;
int len = 1;
for (int x = (1 << 17); x >= 1; x >>= 1)
{
int L = r - (len + x) + 1;
if (L < l) continue;
vector<int> res1 = Ask(L), res2 = Ask(r);
if (res1[1] == res2[1])
len += x;
}
if (l <= r - len)
return Solve(l, r - len);
return -1;
/*for (int len = 1; len <= r - l + 1; len *= 2)
while (l <= r && !Cheapest(r))
{
vector<int> res = Ask(r);
if (res[0] + res[1] == 0) return r;
--r;
}
if (l >= r) return -1;
vector<int> res1 = Ask(l), res2 = Ask(r);
if (res1[1] == res2[1]) return -1;
int m = (l + r) / 2;
int x = Solve(m + 1, r);
if (x != -1) return x;
return Solve(l, m);*/
}
}
int find_best(int n_) {
n = n_;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
shuffle(p.begin(), p.end(), rng);
if (p.size() > 500) p.erase(p.begin() + 500, p.end());
for (int i: p)
{
vector<int> res = Ask(i);
mx = max(mx, res[0] + res[1]);
if (res[0] + res[1] == 0) return i;
}
return Solve(0, n - 1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
5880 KB |
Integer -1 violates the range [0, 199999] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
5888 KB |
Output is correct |
2 |
Incorrect |
18 ms |
5888 KB |
Integer -1 violates the range [0, 199999] |
3 |
Halted |
0 ms |
0 KB |
- |