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>
#include "messy.h"
using namespace std;
#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 2e5 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;
set<string> al;
set<string> after;
vi perm;
#ifdef ACMX
void add_element(std::string x){
al.insert(x);
};
bool check_element(std::string x){
return al.count(x);
}
void compile_set(){
trav(x, al){
string y = x;
F0R(i, siz(x)){
y[i] = x[perm[i]];
}
after.insert(y);
}
swap(after, al);
};
#endif
int n;
vi ans;
void go(int l, int r){
if(r-l==0) return;
string chk(n, '0');
F0R(i, l) chk[i] = '1';
FOR(i, r+1, n) chk[i] = '1';
int m = (l+r)/2;
FOR(i, l, m+1){
chk[i] = '1';
add_element(chk);
chk[i] = '0';
}
go(l, m); go(m+1, r);
}
void solve(int l, int r, set<int> pos){
if(siz(pos)==1){
assert(l==r);
ans[*pos.begin()] = r;
return;
}
string chk(n, '0');
F0R(i, n) if(!pos.count(i)) chk[i] = '1';
int m = (l+r)/2;
set<int> posnewL;
set<int> posnewR;
trav(x, pos){
chk[x] = '1';
if(check_element(chk)) posnewL.insert(x);
chk[x] = '0';
}
trav(x, pos){
if(!posnewL.count(x)) posnewR.insert(x);
}
solve(l, m, posnewL); solve(m+1, r, posnewR);
}
vi restore_permutation(int _n, int w=69, int r=69){
n = _n;
go(0, n-1);
//trav(x, al) cout << x << endl;
compile_set();
//cout << "------\n";
//trav(x, al) cout << x << endl;
set<int> o; F0R(i, n) o.insert(i);
ans = vi(n);
solve(0, n-1, o);
return ans;
}
//int32_t main(){
// ios_base::sync_with_stdio(0); cin.tie(0);
// cin >> n;
// perm.resize(n);
// F0R(i, n) cin >> perm[i];
// vi res = restore_permutation(n);
// //trav(x, al) cout << x << nl;
// trav(x, res) cout << x << ' ' ;
// cout << nl;
// return 0;
//}
# | 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... |