aboutsummaryrefslogtreecommitdiff
path: root/12_interactive_problems/hidden_permutation_3139.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
commit96254947699986c59f0dc63d69fd4b76bd3ed43e (patch)
tree6c4dca945d7f7427c48be234d827fe4d33be02c5 /12_interactive_problems/hidden_permutation_3139.cpp
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
Diffstat (limited to '12_interactive_problems/hidden_permutation_3139.cpp')
-rw-r--r--12_interactive_problems/hidden_permutation_3139.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/12_interactive_problems/hidden_permutation_3139.cpp b/12_interactive_problems/hidden_permutation_3139.cpp
new file mode 100644
index 0000000..fc01d53
--- /dev/null
+++ b/12_interactive_problems/hidden_permutation_3139.cpp
@@ -0,0 +1,45 @@
1#include <iostream>
2#include <iterator>
3#include <string>
4#include <vector>
5
6void print_sol(const std::vector<size_t>& v) {
7 std::cout << "! ";
8 for (size_t i = 0; i < v.size(); i++) {
9 for (size_t j = 0; j < v.size(); j++) {
10 if (v[j] == i) {
11 std::cout << (j+1) << " ";
12 break;
13 }
14 }
15 }
16 std::cout << std::endl;
17}
18
19bool cmp(size_t i, size_t j) {
20 std::cout << "? " << (i+1) << " " << (j+1) << std::endl;
21 std::string s;
22 std::cin >> s;
23 return s == "YES";
24}
25
26size_t binsearch(size_t i, const std::vector<size_t>& v, size_t l, size_t r) {
27 if (r == l) return l;
28 size_t m = (l+r)/2;
29 return cmp(i, v[m]) ? binsearch(i, v, l, m) : binsearch(i, v, m+1, r);
30}
31
32int main() {
33 size_t n;
34 std::vector<size_t> v;
35 std::cin >> n;
36 v.reserve(n);
37
38 v.push_back(0);
39 for (size_t i = 1; i < n; i++) {
40 size_t pos = binsearch(i, v, 0, v.size());
41 v.insert(std::next(v.begin(), pos), i);
42 }
43
44 print_sol(v);
45}

Generated with cgit - Back to sebastiano.tronto.net