#include "koala.h"
#include <bits/stdc++.h>
using namespace std;
#define FO(i,a,b) for (int i = (a); i < (b); i++)
#define pb push_back
#define sz(v) ((int)v.size())
struct Query {
vector <int> am;
};
struct Response {
vector <bool> v;
};
int N, W;
Response makeQuery (Query q) {
int *B = (int *)malloc(sizeof(int)*N);
FO (i,0,N) B[i] = q.am[i];
int *R = (int *)malloc(sizeof(int)*N);
playRound(B,R);
Response r;
FO (i,0,N) {
if (R[i] > B[i]) {
r.v.pb(true);
} else {
r.v.pb(false);
}
}
free(B);
free(R);
return r;
}
Response makeUniformQuery (set<int> &cand, int am) {
Query q;
FO (i,0,N) {
if (cand.find(i) != cand.end()) {
q.am.pb(am);
} else {
q.am.pb(0);
}
}
return makeQuery(q);
}
set<int> responseToSet (Response r) {
set <int> s;
FO (i,0,N) if (r.v[i]) s.insert(i);
return s;
}
int minValue(int _N, int _W) {
N = _N; W = _W;
set <int> cand;
cand.insert(0);
Response r = makeUniformQuery(cand, 1);
FO (i,0,N) {
if (!r.v[i]) return i;
}
assert (false);
}
int actualGetMax() {
set <int> cand;
FO (i,0,N) cand.insert(i);
while (cand.size() > 1) {
Response r = makeUniformQuery(cand, W/(int)cand.size());
set <int> newC;
FO (i,0,N) if (cand.find(i) != cand.end() && r.v[i]) newC.insert(i);
cand = newC;
}
return *(cand.begin());
}
int maxValue(int _N, int _W) {
N = _N; W = _W;
return actualGetMax();
}
int greaterValue(int _N, int _W) {
N = _N; W = _W;
set <int> f2 = {0,1};
int cands[4] = {1,3,6,8};
int lo = 0;
int hi = 3;
int mid;
while (lo <= hi) {
mid = (lo+hi)/2;
Response r = makeUniformQuery(f2, cands[mid]);
set <int> origRes = responseToSet(r);
set <int> res;
for (auto c : f2) {
if (origRes.find(c) != origRes.end()) res.insert(c);
}
switch (res.size()) {
case 0:
hi = mid-1;
break;
case 1:
return *(res.begin());
case 2:
lo = mid+1;
break;
}
}
assert(false);
}
vector <int> sortRange (vector <int> vals);
void solveMain(int *P);
void allValues(int _N, int _W, int *P) {
N = _N; W = _W;
if (_W == 2*_N) {
vector <int> startVals;
FO (i,0,N) startVals.pb(i);
vector <int> endVals = sortRange(startVals);
FO (i,0,N) P[endVals[i]] = N-i;
} else {
solveMain(P);
}
}
// returns greater of the 2
int sub4cmp(int a, int b) {
set<int> vals = {a,b};
set<int> takens = responseToSet(makeUniformQuery(vals, W/2));
if (takens.find(a) != takens.end()) return a;
else return b;
}
vector <int> sortRange (vector <int> vals) {
if (vals.size() == 1) return vals;
vector <int> fV, sV;
FO (i,0,sz(vals)) {
if (i%2) sV.pb(vals[i]);
else fV.pb(vals[i]);
}
vector <int> sF = sortRange(fV);
vector <int> sS = sortRange(sV);
int fI(0), sI(0);
vector <int> retV;
while (fI < sz(sF) || sI < sz(sS)) {
if (fI == sz(sF)) retV.pb(sS[sI++]);
else if (sI == sz(sS)) retV.pb(sF[fI++]);
else {
if (sub4cmp(sF[fI], sS[sI]) == sF[fI]) {
retV.pb(sF[fI++]);
} else {
retV.pb(sS[sI++]);
}
}
}
return retV;
}
set <int> lowestVals;
#define N_BUCKETS 9
#define B_SIZ 10
vector <int> startBuckets[N_BUCKETS];
vector <int> sortedB[N_BUCKETS];
int valToPos[105];
int maxInSet(set<int> &s) {
// Need this:
assert(s.size() <= 9);
set<int> qSet(s);
for (int i = 10; i >= 1; i--) {
if (qSet.size() == 9) break;
qSet.insert(valToPos[i]);
}
set<int> taken;
if (s.size() == 1) {
taken = responseToSet(makeUniformQuery(qSet, 10));
} else {
taken = responseToSet(makeUniformQuery(qSet, 11));
}
for (int v : s) {
if (taken.find(v) != taken.end()) return v;
}
assert(false);
}
void solveMain (int *P) {
lowestVals.clear();
FO (i,0,N_BUCKETS) {
startBuckets[i].clear();
sortedB[i].clear();
}
int mx = actualGetMax();
set<int> mxSet({mx});
set<int> notLowestVals =
responseToSet(makeUniformQuery(mxSet, 10));
FO (i,0,N) {
if (notLowestVals.count(i) == 0) lowestVals.insert(i);
}
assert(lowestVals.size() == 10);
FO (i,0,N) {
if (lowestVals.find(i) != lowestVals.end()) continue;
FO (b,0,N_BUCKETS) {
if (startBuckets[b].size() < B_SIZ) {
startBuckets[b].pb(i);
break;
}
}
}
// Get bottom 10:
set <int> lowLeft (lowestVals);
while (lowLeft.size()) {
if (lowLeft.size() == 1) {
valToPos[1] = *lowLeft.begin();
break;
}
set <int> res =
responseToSet(makeUniformQuery(lowLeft, lowLeft.size()-1));
for (int v : res) {
if (lowLeft.find(v) != lowLeft.end()) {
valToPos[lowLeft.size()] = v;
lowLeft.erase(v);
break;
}
}
}
FO (b,0,N_BUCKETS) {
set <int> remThings(startBuckets[b].begin(), startBuckets[b].end());
while (remThings.size()) {
if (remThings.size() == 10) {
// one special case:
set <int> res = responseToSet(makeUniformQuery(remThings, 10));
set <int> cMax;
for (int v : res) {
if (remThings.find(v) != remThings.end()) {
cMax.insert(v);
}
}
if (cMax.size() == 2) {
int mx = maxInSet(cMax);
sortedB[b].pb(mx);
cMax.erase(mx);
sortedB[b].pb(*cMax.begin());
remThings.erase(mx);
remThings.erase(*cMax.begin());
} else if (cMax.size() == 1) {
sortedB[b].pb(*cMax.begin());
remThings.erase(*cMax.begin());
} else {
assert(false);
}
} else {
int mx;
if (remThings.size() == 1) mx = *remThings.begin();
else mx = maxInSet(remThings);
sortedB[b].pb(mx);
remThings.erase(mx);
}
}
}
// Merge buckets:
// Sort buckets into increasing order not decreasing:
FO (i,0,N_BUCKETS) reverse(sortedB[i].begin(), sortedB[i].end());
vector <int> sortedAll;
while(true) {
set <int> cFronts;
FO (i,0,N_BUCKETS) {
if (sortedB[i].size()) {
cFronts.insert(sortedB[i].back());
}
}
if (cFronts.empty()) break;
int mx = maxInSet(cFronts);
sortedAll.pb(mx);
FO (i,0,N_BUCKETS) {
if (sortedB[i].size() && sortedB[i].back() == mx) {
sortedB[i].pop_back();
}
}
}
for (int i = 10; i >= 1; i--) {
sortedAll.pb(valToPos[i]);
}
FO (i,0,sz(sortedAll)) {
P[sortedAll[i]] = N-i;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
504 KB |
Output is correct |
2 |
Correct |
8 ms |
512 KB |
Output is correct |
3 |
Correct |
10 ms |
512 KB |
Output is correct |
4 |
Correct |
8 ms |
628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
696 KB |
Output is correct |
2 |
Correct |
26 ms |
696 KB |
Output is correct |
3 |
Correct |
24 ms |
696 KB |
Output is correct |
4 |
Correct |
22 ms |
696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
696 KB |
Output is correct |
2 |
Correct |
116 ms |
696 KB |
Output is correct |
3 |
Correct |
145 ms |
796 KB |
Output is correct |
4 |
Correct |
124 ms |
796 KB |
Output is correct |
5 |
Correct |
109 ms |
796 KB |
Output is correct |
6 |
Correct |
123 ms |
800 KB |
Output is correct |
7 |
Correct |
120 ms |
816 KB |
Output is correct |
8 |
Correct |
117 ms |
816 KB |
Output is correct |
9 |
Correct |
132 ms |
816 KB |
Output is correct |
10 |
Correct |
120 ms |
816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
816 KB |
Output is correct |
2 |
Correct |
46 ms |
816 KB |
Output is correct |
3 |
Correct |
50 ms |
816 KB |
Output is correct |
4 |
Correct |
50 ms |
816 KB |
Output is correct |
5 |
Correct |
52 ms |
816 KB |
Output is correct |
6 |
Correct |
66 ms |
816 KB |
Output is correct |
7 |
Correct |
49 ms |
816 KB |
Output is correct |
8 |
Correct |
58 ms |
816 KB |
Output is correct |
9 |
Correct |
68 ms |
816 KB |
Output is correct |
10 |
Correct |
58 ms |
816 KB |
Output is correct |
11 |
Correct |
51 ms |
816 KB |
Output is correct |
12 |
Correct |
53 ms |
816 KB |
Output is correct |
13 |
Correct |
50 ms |
816 KB |
Output is correct |
14 |
Correct |
73 ms |
816 KB |
Output is correct |
15 |
Correct |
55 ms |
816 KB |
Output is correct |
16 |
Correct |
61 ms |
816 KB |
Output is correct |
17 |
Correct |
57 ms |
816 KB |
Output is correct |
18 |
Correct |
54 ms |
816 KB |
Output is correct |
19 |
Correct |
48 ms |
816 KB |
Output is correct |
20 |
Correct |
58 ms |
816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Partially correct |
12 ms |
816 KB |
Output is partially correct |
2 |
Partially correct |
14 ms |
816 KB |
Output is partially correct |
3 |
Partially correct |
12 ms |
816 KB |
Output is partially correct |
4 |
Partially correct |
14 ms |
816 KB |
Output is partially correct |
5 |
Partially correct |
13 ms |
816 KB |
Output is partially correct |
6 |
Partially correct |
16 ms |
816 KB |
Output is partially correct |
7 |
Partially correct |
13 ms |
816 KB |
Output is partially correct |
8 |
Partially correct |
13 ms |
816 KB |
Output is partially correct |
9 |
Partially correct |
12 ms |
816 KB |
Output is partially correct |
10 |
Partially correct |
11 ms |
816 KB |
Output is partially correct |
11 |
Partially correct |
17 ms |
816 KB |
Output is partially correct |
12 |
Partially correct |
13 ms |
816 KB |
Output is partially correct |
13 |
Partially correct |
14 ms |
816 KB |
Output is partially correct |
14 |
Partially correct |
15 ms |
816 KB |
Output is partially correct |
15 |
Partially correct |
19 ms |
816 KB |
Output is partially correct |
16 |
Partially correct |
13 ms |
816 KB |
Output is partially correct |
17 |
Partially correct |
15 ms |
816 KB |
Output is partially correct |
18 |
Partially correct |
14 ms |
816 KB |
Output is partially correct |
19 |
Partially correct |
13 ms |
816 KB |
Output is partially correct |
20 |
Partially correct |
16 ms |
816 KB |
Output is partially correct |
21 |
Partially correct |
16 ms |
816 KB |
Output is partially correct |
22 |
Partially correct |
12 ms |
816 KB |
Output is partially correct |
23 |
Partially correct |
13 ms |
816 KB |
Output is partially correct |
24 |
Partially correct |
12 ms |
816 KB |
Output is partially correct |
25 |
Partially correct |
16 ms |
816 KB |
Output is partially correct |
26 |
Partially correct |
14 ms |
816 KB |
Output is partially correct |
27 |
Partially correct |
15 ms |
816 KB |
Output is partially correct |
28 |
Partially correct |
11 ms |
816 KB |
Output is partially correct |
29 |
Partially correct |
11 ms |
816 KB |
Output is partially correct |
30 |
Partially correct |
11 ms |
816 KB |
Output is partially correct |
31 |
Partially correct |
13 ms |
816 KB |
Output is partially correct |
32 |
Partially correct |
12 ms |
816 KB |
Output is partially correct |
33 |
Partially correct |
14 ms |
816 KB |
Output is partially correct |
34 |
Partially correct |
18 ms |
816 KB |
Output is partially correct |
35 |
Partially correct |
12 ms |
816 KB |
Output is partially correct |
36 |
Partially correct |
15 ms |
816 KB |
Output is partially correct |
37 |
Partially correct |
10 ms |
816 KB |
Output is partially correct |
38 |
Partially correct |
11 ms |
816 KB |
Output is partially correct |
39 |
Partially correct |
18 ms |
848 KB |
Output is partially correct |
40 |
Partially correct |
12 ms |
848 KB |
Output is partially correct |