Skip to content

Commit 6be633d

Browse files
bkoi cmp
1 parent ad48b46 commit 6be633d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

BkOI/11-cmp.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
- Use 4 writes for remember, and 6 reads for compare
3+
- Split 0...4095 into groups of 373. Write 1 to the group that contains a, and leave everything else alone.
4+
- In the compare function, query the group that contains b.
5+
- If this group is set to 0, then we query the remaining groups on the smaller side of b to determine
6+
whether a is larger or smaller than b. There are 11 groups. Worst case we will need six queries (middle group + 5 groups on one side)
7+
- If this group is not set to 0, then a and b are both in the same group of length 373.
8+
- Now we have the same problem but with fewer reads/writes and a smaller range.
9+
- We'll split the group of size 373 into groups of size 42, and run the same algorithm.
10+
There are 9 such groups, so compare() will use at most 5 queries.
11+
- If they're both in the same group of size 42, then we'll split it into groups of size 6, then finally groups of size 1.
12+
*/
13+
14+
#include "cmp.h"
15+
#include <bits/stdc++.h>
16+
17+
using namespace std;
18+
19+
void remember(int n) {
20+
vector<int> gaps = { 373, 42, 6, 1 };
21+
int cur = 0;
22+
int end = 4096;
23+
int nxt = -1;
24+
int nxtEnd = -1;
25+
vector<int> shift = { 1, 12, 21, 28, 34 };
26+
for (int i = 0; i < 4; i++) {
27+
int idx = shift[i];
28+
while (cur < end) {
29+
if (cur <= n && n < cur+gaps[i]) {
30+
bit_set(idx);
31+
nxt = cur;
32+
nxtEnd = cur + gaps[i];
33+
}
34+
cur += gaps[i];
35+
idx++;
36+
}
37+
cur = nxt;
38+
end = nxtEnd;
39+
}
40+
}
41+
42+
int compare(int b) {
43+
vector<int> gaps = { 373, 42, 6, 1 };
44+
// vector<int> shift = { 11, 9, 7, 6 };
45+
vector<int> shift = { 1, 12, 21, 28, 34 };
46+
int cur = 0;
47+
for (int i = 0; i < 4; i++) {
48+
int idx = shift[i];
49+
while (true) {
50+
if (cur <= b && b < cur + gaps[i]) {
51+
break;
52+
}
53+
cur += gaps[i];
54+
idx++;
55+
}
56+
if (!bit_get(idx)) {
57+
int lowerIndex = shift[i], upperIndex = shift[i+1]-1;
58+
if (idx - lowerIndex < upperIndex - idx) {
59+
// ask [lowerIndex...index)
60+
while (lowerIndex < idx) {
61+
if (bit_get(lowerIndex)) return 1;
62+
lowerIndex++;
63+
}
64+
return -1;
65+
} else {
66+
// ask (index...upperIndex]
67+
idx++;
68+
while (idx <= upperIndex) {
69+
if (bit_get(idx)) return -1;
70+
idx++;
71+
}
72+
return 1;
73+
}
74+
}
75+
}
76+
return 0;
77+
}
78+

0 commit comments

Comments
 (0)