#include <vector>
#include "communication.h"
typedef long long ll;
using namespace std;
struct interval {
ll l, r;
interval(ll l, ll r) : l(l), r(r) {}
bool contains(ll x) {
return l <= x && x < r;
}
ll length() {
return r - l;
}
interval shrink(ll o, ll s) {
return interval(min(r, l + o), min(r, l + o + s));
}
};
vector<interval> splitIntervals(vector<interval> c, ll o, ll s) {
vector<interval> r;
for (interval &it : c) {
if (s > 0 && it.length() - o > 0)
r.push_back(it.shrink(o, s));
s -= max(0LL, min(s, it.length() - o));
o -= min(o, it.length());
}
return r;
}
bool contains(vector<interval> c, ll x) {
for (interval &it : c)
if (it.contains(x))
return true;
return false;
}
struct state {
ll s = 0;
vector<interval> c;
state() {}
state(ll N): s(N) {
c.emplace_back(1, N + 1);
}
state(vector<vector<interval>> &cs) {
for (vector<interval> &c : cs)
add(c);
}
void add(interval it) {
s += it.length();
c.push_back(it);
}
void add(vector<interval> &c) {
for (interval &it : c)
add(it);
}
vector<vector<interval>> split() {
vector<vector<interval>> r;
ll s1 = s / 3, s2 = (s - s1) / 2, s3 = s - s1 - s2;
r.push_back(splitIntervals(c, 0, s1));
r.push_back(splitIntervals(c, s1, s2));
r.push_back(splitIntervals(c, s1 + s2, s3));
return r;
}
ll size() {
return s;
}
vector<ll> remaining() {
vector<ll> r;
for (interval &it : c)
for (ll v = it.l; v < it.r; v++)
r.push_back(v);
return r;
}
};
std::pair<int, int> solve(int N, int X = -1) {
state s(N);
while (s.size() > 2) {
vector<vector<interval>> c = s.split();
int b1 = 1, b2;
for (int i = 0; i < 2 && b1; i++)
b1 = X != -1 ? send(contains(c[0], X) || contains(c[1], X)) : receive();
if (!b1)
b2 = X != -1 ? send(contains(c[0], X)) : receive();
c.erase(c.begin() + (b1 ? 2 : (b2 ? 1 : 0)));
s = state(c);
}
vector<ll> r = s.remaining();
return {r[0], r.back()};
}
void encode(int N, int X) {
solve(N, X);
}
std::pair<int, int> decode(int N) {
return solve(N);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
1744 KB |
Output is correct |
2 |
Correct |
6 ms |
1684 KB |
Output is correct |
3 |
Correct |
14 ms |
1732 KB |
Output is correct |
4 |
Correct |
6 ms |
1748 KB |
Output is correct |
5 |
Correct |
12 ms |
1760 KB |
Output is correct |
6 |
Correct |
24 ms |
1932 KB |
Output is correct |
7 |
Correct |
42 ms |
1804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
870 ms |
1712 KB |
Output is partially correct |
2 |
Partially correct |
293 ms |
1760 KB |
Output is partially correct |
3 |
Partially correct |
710 ms |
1784 KB |
Output is partially correct |
4 |
Partially correct |
1090 ms |
1744 KB |
Output is partially correct |
5 |
Partially correct |
810 ms |
1680 KB |
Output is partially correct |
6 |
Partially correct |
658 ms |
1724 KB |
Output is partially correct |
7 |
Partially correct |
2488 ms |
1724 KB |
Output is partially correct |
8 |
Partially correct |
4054 ms |
1984 KB |
Output is partially correct |
9 |
Partially correct |
3299 ms |
1872 KB |
Output is partially correct |
10 |
Partially correct |
3242 ms |
1940 KB |
Output is partially correct |
11 |
Partially correct |
3690 ms |
1828 KB |
Output is partially correct |
12 |
Partially correct |
3568 ms |
1872 KB |
Output is partially correct |
13 |
Partially correct |
3470 ms |
1936 KB |
Output is partially correct |
14 |
Partially correct |
3509 ms |
1884 KB |
Output is partially correct |
15 |
Partially correct |
2078 ms |
1748 KB |
Output is partially correct |
16 |
Partially correct |
4705 ms |
1812 KB |
Output is partially correct |
17 |
Partially correct |
1128 ms |
1772 KB |
Output is partially correct |
18 |
Partially correct |
1084 ms |
1928 KB |
Output is partially correct |
19 |
Partially correct |
1058 ms |
1912 KB |
Output is partially correct |
20 |
Partially correct |
993 ms |
1900 KB |
Output is partially correct |
21 |
Partially correct |
1122 ms |
1704 KB |
Output is partially correct |
22 |
Partially correct |
1224 ms |
1784 KB |
Output is partially correct |
23 |
Partially correct |
1659 ms |
1716 KB |
Output is partially correct |
24 |
Correct |
8 ms |
1732 KB |
Output is correct |
25 |
Correct |
11 ms |
1848 KB |
Output is correct |
26 |
Correct |
13 ms |
1768 KB |
Output is correct |
27 |
Correct |
10 ms |
1688 KB |
Output is correct |
28 |
Correct |
12 ms |
1744 KB |
Output is correct |
29 |
Correct |
23 ms |
1896 KB |
Output is correct |
30 |
Correct |
34 ms |
1988 KB |
Output is correct |