#include "advisor.h"
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
struct Segtree {
int n;
vector<pair<int, int>> st;
Segtree(const vector<int>& a) {
n = a.size();
st = vector<pair<int, int>>(4*n);
build(1, 0, n-1, a);
}
void build(int p, int l, int r, const vector<int>& a) {
if (l == r) st[p] = {a[l], l};
else {
int m = (l+r)/2;
build(2*p, l, m, a);
build(2*p+1, m+1, r, a);
if (st[2*p] > st[2*p+1]) st[p] = st[2*p];
else st[p] = st[2*p+1];
}
}
void change(int p, int l, int r, int i, int v) {
if (l == r) st[p] = {v, i};
else {
int m = (l+r)/2;
if (i <= m) change(2*p, l, m, i, v);
else change(2*p+1, m+1, r, i, v);
if (st[2*p] > st[2*p+1]) st[p] = st[2*p];
else st[p] = st[2*p+1];
}
}
pair<int, int> Max(int p, int l, int r, int i, int j) {
if (i > j) return {0, -1};
if (i == l && j == r) return st[p];
int m = (l+r)/2;
pair<int, int> a = Max(2*p, l, m, i, min(m, j));
pair<int, int> b = Max(2*p+1, m+1, r, max(i, m+1), j);
if (a.first > b.first) return a;
return b;
}
void change(int i, int v) { change(1, 0, n-1, i, v); }
int idx() { return Max(1, 0, n-1, 0, n-1).second; }
};
vector<int> LeonardoStrategy(int *C, int N, int K) {
vector<int> last_pos(N, N);
vector<int> next_pos(N);
for (int i = N-1; i >= 0; --i) {
next_pos[i] = last_pos[C[i]];
last_pos[C[i]] = i;
}
vector<int> pos_on_scaffold(N, -1);
for (int i = 0; i < K; ++i) pos_on_scaffold[i] = i;
vector<int> scaffold(K);
vector<int> next(K);
for (int i = 0; i < K; ++i) {
next[i] = last_pos[i];
scaffold[i] = i;
}
Segtree Scaffold(next);
vector<int> optimal_strategy;
for (int i = 0; i < N; ++i) {
if (pos_on_scaffold[C[i]] != -1) {
Scaffold.change(pos_on_scaffold[C[i]], next_pos[i]);
} else {
int idx = Scaffold.idx();
optimal_strategy.pb(idx);
Scaffold.change(idx, next_pos[i]);
pos_on_scaffold[scaffold[idx]] = -1;
pos_on_scaffold[C[i]] = idx;
scaffold[idx] = C[i];
}
}
return optimal_strategy;
}
void ComputeAdvice(int *C, int N, int K, int M) {
vector<int> optimal_strategy = LeonardoStrategy(C, N, K);
int log_k = 0;
while ((1<<(log_k+1) <= K)) ++log_k;
for (int i : optimal_strategy) {
for (int j = log_k; j >= 0; --j) {
if ((i&(1<<j))) WriteAdvice(1);
else WriteAdvice(0);
}
}
}
#include "assistant.h"
#include <bits/stdc++.h>
using namespace std;
void Assist(unsigned char *A, int N, int K, int R) {
vector<int> scaffold(K);
vector<int> pos_on_scaffold(N, -1);
for (int i = 0; i < K; ++i) {
scaffold[i] = i;
pos_on_scaffold[i] = i;
}
int log_k = 0;
while (1<<(log_k+1) <= K) ++log_k;
int j = 0;
for (int i = 0; i < N; ++i) {
int c = GetRequest();
if (pos_on_scaffold[c] != -1) continue;
int idx = 0;
for (int k = log_k; k >= 0; --k) {
if (A[j] == 1) idx |= (1<<k);
++j;
}
PutBack(scaffold[idx]);
pos_on_scaffold[scaffold[idx]] = -1;
scaffold[idx] = c;
pos_on_scaffold[c] = idx;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
792 KB |
Output is correct |
2 |
Correct |
0 ms |
796 KB |
Output is correct |
3 |
Correct |
1 ms |
800 KB |
Output is correct |
4 |
Correct |
2 ms |
832 KB |
Output is correct |
5 |
Correct |
2 ms |
1092 KB |
Output is correct |
6 |
Correct |
5 ms |
1152 KB |
Output is correct |
7 |
Correct |
3 ms |
836 KB |
Output is correct |
8 |
Correct |
6 ms |
1164 KB |
Output is correct |
9 |
Correct |
6 ms |
1168 KB |
Output is correct |
10 |
Correct |
6 ms |
1252 KB |
Output is correct |
11 |
Correct |
6 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
1516 KB |
Output is correct |
2 |
Correct |
45 ms |
4432 KB |
Output is correct |
3 |
Correct |
126 ms |
10264 KB |
Output is correct |
4 |
Correct |
100 ms |
7944 KB |
Output is correct |
5 |
Correct |
115 ms |
9884 KB |
Output is correct |
6 |
Correct |
134 ms |
10088 KB |
Output is correct |
7 |
Correct |
114 ms |
9516 KB |
Output is correct |
8 |
Correct |
114 ms |
9584 KB |
Output is correct |
9 |
Correct |
57 ms |
6644 KB |
Output is correct |
10 |
Correct |
103 ms |
8704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
7360 KB |
Output is correct |
2 |
Correct |
111 ms |
8832 KB |
Output is correct |
3 |
Correct |
106 ms |
8648 KB |
Output is correct |
4 |
Correct |
104 ms |
8560 KB |
Output is correct |
5 |
Correct |
82 ms |
7252 KB |
Output is correct |
6 |
Correct |
138 ms |
8740 KB |
Output is correct |
7 |
Correct |
110 ms |
8616 KB |
Output is correct |
8 |
Correct |
155 ms |
11520 KB |
Output is correct |
9 |
Correct |
68 ms |
7416 KB |
Output is correct |
10 |
Correct |
126 ms |
8684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1160 KB |
Error - advice is too long |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
9528 KB |
Output is partially correct - 772365 bits used |
2 |
Correct |
110 ms |
8832 KB |
Output is partially correct - 742095 bits used |
3 |
Correct |
111 ms |
8672 KB |
Output is partially correct - 712470 bits used |
4 |
Correct |
114 ms |
8840 KB |
Output is partially correct - 712005 bits used |
5 |
Correct |
107 ms |
8672 KB |
Output is partially correct - 710610 bits used |
6 |
Correct |
107 ms |
8752 KB |
Output is partially correct - 712155 bits used |
7 |
Correct |
108 ms |
8680 KB |
Output is partially correct - 711090 bits used |
8 |
Correct |
107 ms |
8856 KB |
Output is partially correct - 713340 bits used |
9 |
Correct |
122 ms |
8764 KB |
Output is partially correct - 712830 bits used |
10 |
Correct |
149 ms |
11316 KB |
Output is partially correct - 1117620 bits used |