-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathioi16p5.cpp
71 lines (56 loc) · 1.45 KB
/
ioi16p5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Ivan Carvalho
// Solution to https://dmoj.ca/problem/ioi16p5
#include <bits/stdc++.h>
using namespace std;
int whichNumber[129];
int N;
void solve_add(int left, int right) {
if (left == right) return;
int mid = left + (right - left) / 2;
string s;
for (int i = 0; i < N; i++) {
if (left <= i && i <= right) {
s.push_back('0');
} else {
s.push_back('1');
}
}
for (int i = left; i <= mid; i++) {
s[i] = '1';
add_element(s);
s[i] = '0';
}
solve_add(left, mid);
solve_add(mid + 1, right);
}
void solve_check(int left, int right, vector<int> possible) {
if (left == right) {
whichNumber[possible[0]] = left;
return;
}
int mid = left + (right - left) / 2;
vector<int> firstHalf, lastHalf;
string s;
for (int i = 0; i < N; i++) s.push_back('1');
for (int i : possible) s[i] = '0';
for (int i : possible) {
s[i] = '1';
if (check_element(s)) {
firstHalf.push_back(i);
} else {
lastHalf.push_back(i);
}
s[i] = '0';
}
solve_check(left, mid, firstHalf);
solve_check(mid + 1, right, lastHalf);
}
int* restore_permutation(int n, int w, int r) {
N = n;
vector<int> allElements;
for (int i = 0; i < N; i++) allElements.push_back(i);
solve_add(0, N - 1);
compile_set();
solve_check(0, N - 1, allElements);
return whichNumber;
}