-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3139 - Hidden Permutation.cpp
More file actions
70 lines (61 loc) · 1.28 KB
/
3139 - Hidden Permutation.cpp
File metadata and controls
70 lines (61 loc) · 1.28 KB
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
#include <bits/stdc++.h>
using namespace std;
#define input "input.in"
#define output "output.out"
bool ask(int i, int j) {
cout << "? " << i + 1 << " " << j + 1 << "\n";
cout.flush();
string ans;
cin >> ans;
return ans == "YES";
}
void answer(vector<int>& p) {
cout << "! ";
vector<int> ans(p.size());
for (int i = 0; i < p.size(); i++) {
ans[p[i]] = i + 1;
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << "\n";
cout.flush();
}
int find_insertion_position(vector<int>& p, int i) {
int l = 0, r = p.size();
while (l < r) {
int m = (l + r) / 2;
if (ask(p[m], i)) {
l = m + 1;
} else {
r = m;
}
}
return l;
}
void print(vector<int>& p) {
cout << "\n";
for (auto i : p) cout << i << " ";
cout << "\n";
}
void solve() {
int n;
cin >> n;
vector<int> p;
for (int i = 0; i < n; i++) {
int idx = find_insertion_position(p, i);
p.insert(p.begin() + idx, i);
// print(p);
}
answer(p);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen(input, "r")) freopen(input, "r", stdin);
if (fopen(output, "r")) freopen(output, "w+", stdout);
int t = 1;
// cin >> t;
while (t--) solve();
}