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 "grader.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define ii pair<ll,ll>
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define lb lower_bound
#define ub upper_bound
#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);(s<e?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
mt19937 rng(342759);
int n;
vector<int> ans;
vector<int> pos; //which pos idk
#define vi pair<vector<int>,int>
void shuf(){
rep(x,0,sz(pos)){
swap(ans[pos[x]],ans[pos[rng()%(x+1)]]);
}
}
const int LIM=2;
void solve(int N) {
n=N;
rep(x,1,n+1) ans.pub(x);
rep(x,0,n) pos.pub(x);
int correct=0;
int num=0;
int curr;
do{
shuf();
curr=query(ans);
num++;
if (curr==n) return;
} while (curr);
while (true){
vector<int> idx;
int avail;
assert(correct==query(ans));
do{
rep(x,0,sz(pos)) swap(pos[x],pos[rng()%(x+1)]);
idx.clear();
for (int x=0;x<sz(pos)-1;x+=2) idx.pub(x);
for (auto &it:idx) swap(ans[pos[it]],ans[pos[it+1]]);
curr=query(ans);
if (curr==n) return;
avail=curr-correct;
for (auto &it:idx) swap(ans[pos[it]],ans[pos[it+1]]);
} while ((avail<LIM && n-correct>5) || avail==0);
//cout<<"avail: "<<avail<<endl;
set<int> bad;
vector<vi> store={vi(idx,avail)};
while (avail){
idx=store.back().fi;
int nc=store.back().se+correct;
//cout<<"idx: "; for (auto &it:idx) cout<<it<<" "; cout<<endl;
//cout<<"nc: "<<nc<<endl;
store.pob();
while (sz(idx)!=1){
int temp=sz(idx)/2;
rep(x,0,temp) swap(ans[pos[idx[x]]],ans[pos[idx[x]+1]]);
curr=query(ans);
if (curr==n) return;
rep(x,0,temp) swap(ans[pos[idx[x]]],ans[pos[idx[x]+1]]);
vector<int> idxl,idxr;
rep(x,0,temp) idxl.pub(idx[x]);
rep(x,temp,sz(idx)) idxr.pub(idx[x]);
if (curr!=correct){
if (nc!=curr) store.pub(vi(idxr,nc-curr));
idx=idxl;
nc=curr;
}
else{
idx=idxr;
nc=nc-(curr-correct);
}
}
swap(ans[pos[idx[0]]],ans[pos[idx[0]+1]]);
//cout<<"swap: "<<pos[idx[0]]<<" "<<pos[idx[0]+1]<<endl;
curr=nc;
if (curr==correct+2){
bad.insert(pos[idx[0]]);
bad.insert(pos[idx[0]+1]);
correct+=2;
avail-=2;
}
else{
int temp;
for (auto &it:pos){
if (it!=pos[idx[0]] && it!=pos[idx[0]+1]
&& !bad.count(it)){
temp=it;
break;
}
}
//cout<<"temp: "<<temp<<endl;
swap(ans[pos[idx[0]]],ans[temp]);
curr=query(ans);
if (curr==n) return;
swap(ans[pos[idx[0]]],ans[temp]);
if (curr==correct){
bad.insert(pos[idx[0]]);
}
else{
bad.insert(pos[idx[0]+1]);
}
correct++;
avail--;
}
}
vector<int> npos;
for (auto &it:pos) if (!bad.count(it)) npos.pub(it);
swap(pos,npos);
//cout<<"num left: "<<sz(pos)<<endl;
//for (auto &it:ans) cout<<it<<" "; cout<<endl;
//cout<<endl;
//for (auto &it:pos) cout<<it<<" "; cout<<endl;
//cout<<endl;
}
}
Compilation message (stderr)
mouse.cpp: In function 'void solve(int)':
mouse.cpp:137:35: warning: 'temp' may be used uninitialized in this function [-Wmaybe-uninitialized]
137 | swap(ans[pos[idx[0]]],ans[temp]);
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |