This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e18
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
// dp
template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;}
template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;}
//===================//
// begin program //
//===================//
const int MX = 5e5;
const int N = (1<<20);
int query(vi q);
vi findEmpty(int n) {
vi perm;
RE1(i,n) perm.pb(i);
while(true) {
random_shuffle(all(perm),[](int i){return rng()%i;});
int res = query(perm);
if(res == n) return {};
if(res == 0) return perm;
}
}
void solve(int n) {
vi f = findEmpty(n);
if(f.empty()) return;
vector<vii> rounds; rounds.resize(n);
RE(i,n) RE(j,i) {
int cr = (i+j)%n;
rounds[cr].push_back({i,j});
}
// finding good swaps
vii goodSwaps;
FOR(v,rounds) {
while(true) {
FOR(p,v) swap(f[p.fi], f[p.se]);
int res = query(f);
FOR(p,v) swap(f[p.fi], f[p.se]);
if(res == n) return;
if(res == 0) break;
int lb=1, ub=v.size();
while(lb != ub) {
int mid=(lb+ub)/2;
RE(i,mid) swap(f[v[i].fi], f[v[i].se]);
int nRes = query(f);
RE(i,mid) swap(f[v[i].fi], f[v[i].se]);
if(nRes == n) return;
if(nRes != res) lb=mid+1;
else ub=mid;
}
while(v.size() > lb) v.pop_back();
goodSwaps.pb(v.back());
v.pop_back();
}
}
// finding cycles
vector<vi> adj; adj.resize(n);
FOR(p,goodSwaps) {
adj[p.fi].pb(p.se);
adj[p.se].pb(p.fi);
}
vi ans; ans.assign(n,0);
vector<bool> used; used.assign(n,0);
RE(i,n) {
if(used[i]) continue;
vi cycle = {i};
while(!used[cycle.back()]) {
int u = cycle.back();
int nxt = -1;
FOR(v,adj[u]) if(!used[v]) nxt = v;
used[u] = 1;
if(nxt != -1) cycle.pb(nxt);
}
if(cycle.size() == 2) {
ans[cycle[0]] = f[cycle[1]];
ans[cycle[1]] = f[cycle[0]];
} else {
vi perm = f;
int m = cycle.size();
RE(i,m) perm[cycle[i]] = f[cycle[(i+1)%m]];
int nRes = query(perm);
if(nRes == n) return;
if(nRes) {
RE(i,m) ans[cycle[i]] = f[cycle[(i+1)%m]];
} else {
RE(i,m) ans[cycle[i]] = f[cycle[(i+m-1)%m]];
}
}
}
query(ans);
}
#ifdef debug
int n, totQueries;
vi ans;
int query(vi q) {
int res = 0;
RE(i,n) if(ans[i] == q[i]) res++;
if(res == n) OUTL("Correct!");
totQueries++;
return res;
}
int main() {
IN(n); ans.assign(n,0);
RE(i,n) IN(ans[i]);
solve(n);
OUTL("Total queries: ",totQueries);
}
#endif
Compilation message (stderr)
mouse.cpp: In function 'void solve(int)':
mouse.cpp:102:28: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
102 | while(v.size() > lb) v.pop_back();
| ~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |