// https://oj.uz/problem/view/info1cup19_mouse
#include <bits/stdc++.h>
using namespace std;
namespace std {
template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
static_assert(D >= 1, "Dimension must be positive");
template <typename... Args>
Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
};
template <typename T>
struct Vec<1, T> : public vector<T> {
Vec(int n = 0, T val = T()) : std::vector<T>(n, val) {}
};
/* Example
Vec<4, int64_t> f(n, k, 2, 2); // = f[n][k][2][2];
Vec<2, int> adj(n); // graph
*/
template <class Fun>
class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
/* Example
auto fun = y_combinator([&](auto self, int x) -> void {
self(x + 1);
});
*/
} // namespace std
const int N = 555;
int p[N];
int n;
int queries = 0;
int query(vector<int> q) {
queries++;
int cnt = 0;
for (int i = 0; i < n; i++) cnt += p[i] == q[i];
return cnt;
}
int ask(const vector<int> &q) {
int x = query(q);
if (x == n) {
cout << queries << '\n';
for (int i : q) cout << i << ' ';
cout << '\n';
exit(0);
}
return x;
}
mt19937 rng(303);
void solve(int n) {
::n = n;
vector<int> a(n);
iota(a.begin(), a.end(), 1);
int cur = ask(a);
for (int i = 0; i <= 0; i++) {
for (int j = i + 1; j < n; j++) {
swap(a[i], a[j]);
int tmp = ask(a);
if (tmp > cur) {
cur = tmp;
} else {
swap(a[i], a[j]);
}
}
}
vector<int> candidates;
for (int i = 1; i < n; i++) {
swap(a[i], a[0]);
int x = ask(a);
swap(a[i], a[0]);
if (cur - x == 2) continue;
candidates.emplace_back(i);
}
auto shift = [&](const vector<int> &v, bool invert = 0, bool answer = 1) {
vector<int> b(v.size());
for (int i = 0; i < v.size(); i++) b[i] = a[v[i]];
for (int i = 0; i < v.size(); i++) a[v[i]] = b[(i + 1) % v.size()];
int res = answer ? ask(a) : 0;
if (invert) {
for (int i = 0; i < v.size(); i++) a[v[i]] = b[i];
}
return res;
};
while (true) {
int diff = shift(candidates, 1) - cur;
if (diff == 0) {
shift(candidates, 0, 0);
continue;
}
int last = -1;
vector<int> good(candidates.size());
for (int d = 1; d <= diff; d++) {
int low = last + 1, high = candidates.size() - 1;
while (low < high) {
int mid = low + high >> 1;
vector<int> tmp;
for (int i = 0; i <= mid; i++) tmp.emplace_back(candidates[i]);
if (tmp.size() != candidates.size()) {
tmp.emplace_back(0);
tmp.emplace_back(candidates.back());
}
if (shift(tmp, 1) - cur >= d - (mid != candidates.size() ? 1 : 0)) {
high = mid;
} else {
low = mid + 1;
}
}
last = high;
good[(high + candidates.size() - 1) % candidates.size()] = 1;
}
shift(candidates, 0, 0);
vector<int> ncan;
for (int i = 0; i < candidates.size(); i++) {
if (!good[i]) ncan.emplace_back(candidates[i]);
}
ncan.swap(candidates);
cur += diff;
}
ask(a);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> p[i];
solve(n);
}
Compilation message
mouse.cpp: In lambda function:
mouse.cpp:106:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (int i = 0; i < v.size(); i++) b[i] = a[v[i]];
| ~~^~~~~~~~~~
mouse.cpp:107:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for (int i = 0; i < v.size(); i++) a[v[i]] = b[(i + 1) % v.size()];
| ~~^~~~~~~~~~
mouse.cpp:110:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for (int i = 0; i < v.size(); i++) a[v[i]] = b[i];
| ~~^~~~~~~~~~
mouse.cpp: In function 'void solve(int)':
mouse.cpp:126:47: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
126 | int mid = low + high >> 1;
| ~~~~^~~~~~
mouse.cpp:133:69: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | if (shift(tmp, 1) - cur >= d - (mid != candidates.size() ? 1 : 0)) {
| ~~~~^~~~~~~~~~~~~~~~~~~~
mouse.cpp:144:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for (int i = 0; i < candidates.size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccNQhAwl.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccftYeJi.o:mouse.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccNQhAwl.o: in function `query(std::vector<int, std::allocator<int> >)':
grader.cpp:(.text+0x0): multiple definition of `query(std::vector<int, std::allocator<int> >)'; /tmp/ccftYeJi.o:mouse.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status