#include "Anna.h"
#include <vector>
#include<bits/stdc++.h>
#define cout cerr
using namespace std;
namespace anna{
const int M = 18;
int cnt, n, l, r;
int from, to, ed, ans;
stack<int> st;
vector<int> lst, bit;
int x = -1, y = -1;
int p = 0, mn = 0;
vector<pair<int, int> > arr[20];
int phase = 0;
void build(int d, int l, int r) {
arr[d].emplace_back(l, r);
if (l != r) {
int mid = l + r >> 1;
build(d + 1, l, mid);
build(d + 1, mid + 1, r);
}
}
} // namespace
using namespace anna;
void InitA(int N, int ll, int rr) {
n = N;
l = ll;
r = rr;
build(0, 0, n - 1);
for(int depth = 14; depth >= 0; depth--) {
int cover = -1;
for(int i = 0; i < arr[depth].size(); i++) {
int L, R; tie(L, R) = arr[depth][i];
if (L <= l && r <= R) {
cover = i;
x = L;
y = R;
}
}
if (cover == -1) continue;
for(int i = 0; i < 4; i++) SendA(depth >> i & 1), cnt++;
for(int i = 0; i < depth; i++) SendA(cover >> i & 1), cnt++;
assert((1 << depth) > cover);
// cout << cnt << endl;
// cout << depth << endl;
//// cout << from << " " << to << endl;
// cout << x << " " << y << endl;
break;
}
assert(x >= 0);
from = to = x + y >> 1;
assert(from >= l && to <= r);
while (cnt < M) {
int delta = (y - to + from - x + 1) / 2;
// cout << from << " " << to << " " << x << " " << y << endl;
int tmp;
if (from - x >= delta) {
tmp = from - delta;
assert(tmp >= x);
if (l <= tmp) {
SendA(1);
from = tmp;
}
else {
SendA(0);
x = min(from, tmp + 1);
// x = min(from, from + 1);
}
}
else {
tmp = to + delta;
assert(tmp <= y);
if (tmp <= r) {
SendA(1);
to = tmp;
}
else {
SendA(0);
y = max(to, tmp - 1);
}
}
cnt++;
}
// cout << depth << endl;
// cout << from << " " << to << endl;
// cout << x << " " << y << endl;
// exit(0);
}
void ReceiveA(bool X) {
// cerr << X << endl;
if (phase == 0) bit.push_back(X);
if (phase == 0 && bit.size() == 20) {
for(int i = 0; i < 20; i++) if (bit[i]) mn |= (1 << i);
for(int i = x; i < from; i++) lst.push_back(i);
lst.push_back(mn);
for(int i = to + 1; i <= y; i++) lst.push_back(i);
ed = mn;
for(int i = to + 1; i <= y; i++) if (i <= r) ed = i;
// cout << lst.size() << endl;
// cout << mn << endl;
// exit(0);
phase = 1;
}
else if (phase == 1) {
// cout << ed << endl;
// cout << X << endl;
// exit(0);
if (X == 0) st.pop();
else {
st.push(lst[p]);
// cout << lst[p] << " " << ed << endl;
// exit(0);
if (lst[p] == ed) {
ans = -1;
while (!st.empty() && st.top() >= l) {
ans = st.top();
st.pop();
}
assert(ans >= 0);
// cout << ans << endl;
// exit(0);
phase = 2;
}
p++;
}
}
}
int Answer() {
return ans;
}
#include "Bruno.h"
#include <vector>
#include<bits/stdc++.h>
#define cout cerr
using namespace std;
namespace bruno {
const int N = 1e6 + 10, M = 18;
int n, cnt_anna;
int a[N];
int x, y, from, to;
int phase, depth;
vector<bool> bit;
vector<pair<int, int> > arr[20];
void build(int d, int l, int r) {
arr[d].emplace_back(l, r);
if (l != r) {
int mid = l + r >> 1;
build(d + 1, l, mid);
build(d + 1, mid + 1, r);
}
}
void InitB(int N, std::vector<int> P) {
n = N;
for(int i = 0; i < n; i++) a[i] = P[i];
}
void ReceiveB(bool Y) {
cnt_anna++;
bit.push_back(Y);
if (phase == 0 && bit.size() == 4) {
depth = 0;
for(int i = 0; i < 4; i++) if (bit[i]) depth |= (1 << i);
bit.clear();
phase = 1;
// cout << depth << endl;
// exit(0);
}
if (phase == 1 && bit.size() == depth) {
build(0, 0, n - 1);
int idx = 0;
for(int i = 0; i < depth; i++) if (bit[i]) idx |= (1 << i);
bit.clear();
tie(x, y) = arr[depth][idx];
from = to = x + y >> 1;
phase = 2;
}
if (cnt_anna >= M) {
for(int i = 0; i < bit.size(); i++) {
int delta = (y - to + from - x + 1) / 2;
int tmp;
if (from - x >= delta) {
tmp = from - delta;
if (bit[i]) from = tmp;
else x = min(from, tmp + 1);
}
else {
tmp = to + delta;
if (bit[i]) to = tmp;
else y = max(to, tmp - 1);
}
}
// cout << from << " " << to << " " << x << " " << y << endl;
// exit(0);
vector<int> lst;
for(int i = x; i < from; i++) lst.push_back(a[i]);
int mn = from;
for(int i = from; i <= to; i++) if (a[i] < a[mn]) mn = i;
for(int i = 0; i < 20; i++) SendB(mn >> i & 1);
lst.push_back(a[mn]);
for(int i = to + 1; i <= y; i++) lst.push_back(a[i]);
// cout << lst.size() << endl;
// exit(0);
stack<int> st;
for(int i = 0; i < lst.size(); i++) {
while (!st.empty() && lst[st.top()] >= lst[i]) st.pop(), SendB(0);
SendB(1);
st.push(i);
}
// exit(0);
}
}
} // namespace
void InitB(int N, std::vector<int> P) {
bruno::InitB(N, P);
}
void ReceiveB(bool Y) {
bruno::ReceiveB(Y);
}
Compilation message
Anna.cpp: In function 'void anna::build(int, int, int)':
Anna.cpp:29:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid = l + r >> 1;
| ~~^~~
Anna.cpp: In function 'void InitA(int, int, int)':
Anna.cpp:44:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i = 0; i < arr[depth].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
Anna.cpp:64:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | from = to = x + y >> 1;
| ~~^~~
Bruno.cpp: In function 'void bruno::build(int, int, int)':
Bruno.cpp:25:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid = l + r >> 1;
| ~~^~~
Bruno.cpp: In function 'void bruno::ReceiveB(bool)':
Bruno.cpp:47:32: warning: comparison of integer expressions of different signedness: 'std::vector<bool>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
47 | if (phase == 1 && bit.size() == depth) {
| ~~~~~~~~~~~^~~~~~~~
Bruno.cpp:53:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | from = to = x + y >> 1;
| ~~^~~
Bruno.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<bool>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < bit.size(); i++) {
| ~~^~~~~~~~~~~~
Bruno.cpp:83:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int i = 0; i < lst.size(); i++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
916 KB |
Output is correct |
2 |
Correct |
2 ms |
664 KB |
Output is correct |
3 |
Correct |
0 ms |
676 KB |
Output is correct |
4 |
Correct |
1 ms |
664 KB |
Output is correct |
5 |
Correct |
2 ms |
664 KB |
Output is correct |
6 |
Correct |
12 ms |
784 KB |
Output is correct |
7 |
Correct |
5 ms |
664 KB |
Output is correct |
8 |
Correct |
9 ms |
1176 KB |
Output is correct |
9 |
Correct |
4 ms |
832 KB |
Output is correct |
10 |
Correct |
1 ms |
920 KB |
Output is correct |
11 |
Correct |
2 ms |
664 KB |
Output is correct |
12 |
Correct |
4 ms |
664 KB |
Output is correct |
13 |
Correct |
9 ms |
1036 KB |
Output is correct |
14 |
Correct |
4 ms |
916 KB |
Output is correct |
15 |
Correct |
13 ms |
784 KB |
Output is correct |
16 |
Correct |
5 ms |
664 KB |
Output is correct |
17 |
Correct |
5 ms |
920 KB |
Output is correct |
18 |
Correct |
2 ms |
920 KB |
Output is correct |
19 |
Correct |
2 ms |
664 KB |
Output is correct |
20 |
Correct |
2 ms |
920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
916 KB |
Output is correct |
2 |
Correct |
2 ms |
664 KB |
Output is correct |
3 |
Correct |
0 ms |
676 KB |
Output is correct |
4 |
Correct |
1 ms |
664 KB |
Output is correct |
5 |
Correct |
2 ms |
664 KB |
Output is correct |
6 |
Correct |
12 ms |
784 KB |
Output is correct |
7 |
Correct |
5 ms |
664 KB |
Output is correct |
8 |
Correct |
9 ms |
1176 KB |
Output is correct |
9 |
Correct |
4 ms |
832 KB |
Output is correct |
10 |
Correct |
1 ms |
920 KB |
Output is correct |
11 |
Correct |
2 ms |
664 KB |
Output is correct |
12 |
Correct |
4 ms |
664 KB |
Output is correct |
13 |
Correct |
9 ms |
1036 KB |
Output is correct |
14 |
Correct |
4 ms |
916 KB |
Output is correct |
15 |
Correct |
13 ms |
784 KB |
Output is correct |
16 |
Correct |
5 ms |
664 KB |
Output is correct |
17 |
Correct |
5 ms |
920 KB |
Output is correct |
18 |
Correct |
2 ms |
920 KB |
Output is correct |
19 |
Correct |
2 ms |
664 KB |
Output is correct |
20 |
Correct |
2 ms |
920 KB |
Output is correct |
21 |
Runtime error |
1 ms |
588 KB |
Execution killed with signal 13 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
27 ms |
27716 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |