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;
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
#define all(v) v.begin(),v.end()
#define sort(v) sort(all(v))
#define endl '\n'
#define forn(i,n) for(int i = 0; i < n; ++i)
#define forbe(i,b,e) for(int i = b; i < e; ++i)
#define forr(i,n) for(int i = n-1; i >= 0; --i)
#define sz(v) ((int)v.size())
#define pb push_back
#define f first
#define s second
void add_element(std::string x);
bool check_element(std::string x);
void compile_set();
vi perm;
std::vector<int> sub12(int n)
{
string x;
forn(i,n)
x.pb('0');
forn(i,n-1) {
x[i] = '1';
add_element(x);
}
compile_set();
vector<bool> fnd(n);
vi perm(n);
forn(i,n)
x[i] = '0';
forn(i,n-1) {
forn(j,n) {
if (fnd[j]) continue;
x[j] = '1';
if (check_element(x)) {
fnd[j] = 1;
perm[j] = i;
// cout << j << ' ' << i << endl;
break;
}
x[j] = '0';
}
}
forn(i,n) {
if (fnd[i]) continue;
perm[i] = n-1;
}
return perm;
}
void bs(int l, int r, string s) {
int mid = (l+r)>>1;
if (l == r-1)
return;
forn(i, sz(s))
s[i] = '1';
forbe(i,l,r)
s[i] = '0';
forbe(i,l,mid) {
s[i] = '1';
add_element(s);
s[i] = '0';
}
forbe(i,l,mid)
s[i] = '1';
// forbe(i,mid,r) {
// s[i] = '1';
// add_element(s);
// s[i] = '0';
// }
bs(l,mid,s);
bs(mid,r,s);
}
void bs2(int l, int r, string s, vector<bool> us) {
int mid = (l+r)>>1;
if (l == r-1) {
forn(i,sz(s)) {
if (!us[i]) {
perm[i] = l;
break;
}
}
return;
}
vector<bool> lf, rt;
lf = rt = us;
forn(i, sz(s))
if (us[i])
s[i] = '1';
else
s[i] = '0';
vi le, re;
forn(i, sz(s)) {
if (us[i])
continue;
s[i] = '1';
if (check_element(s)) {
le.pb(i);
rt[i] = 1;
}
else {
re.pb(i);
lf[i] = 1;
}
s[i] = '0';
}
// cout << l << ' ' << r << endl;
// cout << "L:";
// forn(i,sz(le))
// cout << ' ' << le[i];
// cout << endl;
// cout << "R:";
// forn(i,sz(re))
// cout << ' ' << re[i];
// cout << endl;
bs2(l,mid,s,lf);
bs2(mid,r,s,rt);
}
std::vector<int> subrest(int n)
{
string x;
forn(i,n)
x.pb('0');
bs(0,n,x);
compile_set();
vector<bool> fnd(n);
perm.resize(n);
forn(i,n)
perm[i] = i;
// return perm;
bs2(0,n,x,fnd);
return perm;
}
std::vector<int> restore_permutation(int n, int w, int r) {
return subrest(n);
if (n == 8) return sub12(n);
else if (n == 32 && r == 1024) return sub12(n);
return subrest(n);
vi ans;
return ans;
}
#ifdef ONPC
vi rperm;
set<string> nmb;
int _w = 0;
int _r = 0;
int _n;
void add_element(std::string x) {
++ _w;
string rs;
forn(i,sz(x)) {
rs.pb('0');
rs[i] = x[rperm[i]];
}
cout << x << ' ' << rs << endl;
nmb.insert(rs);
}
bool check_element(std::string x) {
// cout << x << ' ' << nmb.count(x) << endl;
++ _r;
return nmb.count(x);}
void compile_set() {}
void solve() {
int n, w, r;
cin >> n >> w >> r;
_n = n;
rperm.resize(n);
forn(i,n)
cin >> rperm[i];
vi ans = restore_permutation(n,w,r);
cout << _w << ' ' << _r << endl;
forn(i,n)
cout << ans[i] << ' ';
cout << endl;
}
int main() {
freopen("in", "r", stdin);
// ios_base::sync_with_stdio(0);cin.tie(0);
solve();
}
#endif
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |