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 ll long long
//#define endl "\n"
const int MOD = 998244353;
/*void add_element(string x);
void compile_set();
bool check_element(string x);*/
void add_lg(int n, int lg){
string s = "";
for(int i = 0; i < n; i++) s += '0';
s[0] = '1';
s[1] = '1';
add_element(s);
s[1] = '0';
s[2] = '1';
s[3] = '1';
add_element(s);
s[1] = '1';
s[2] = '0';
s[3] = '0';
for(int i = 2; i < lg; i++){
s[i] = '1';
add_element(s);
}
}
void check_lg(int n, int lg, vector<int>& possible, vector<int>& ind){
string s = "";
for(int i = 0; i < n; i++) s += '0';
pair<int, int> initial = {-1, -1};
for(int i = 0; i < lg && initial.first == -1; i++){
for(int j = i + 1; j < lg && initial.first == -1; j++){
for(int q = 0; q < lg; q++) s[possible[q]] = '0';
s[possible[i]] = '1';
s[possible[j]] = '1';
bool rep = check_element(s);
if(rep){
initial = {i, j};
}
}
}
for(int i = 2; i < lg; i++){
for(int j = 0; j < lg; j++){
for(int q = 0; q < lg; q++) s[possible[q]] = '0';
for(int q = i - 1; q >= 2; q--) s[ind[q]] = '1';
s[initial.first] = '1';
s[initial.second] = '1';
if(s[possible[j]] == '1') continue;
else if(i == lg - 1){
for(int x : possible){
if(s[x] == '0') ind[i] = x;
}
break;
}
s[possible[j]] = '1';
bool rep = check_element(s);
if(rep){
ind[i] = possible[j];
break;
}
}
}
for(int q = 0; q < lg; q++) s[possible[q]] = '0';
s[initial.first] = '1';
s[ind[2]] = '1';
s[ind[3]] = '1';
bool rep = check_element(s);
if(rep) {ind[0] = initial.first; ind[1] = initial.second;}
else {ind[1] = initial.first; ind[0] = initial.second;}
}
vector<int> restore_permutation(int n, int w, int r){
int lg = 0;
int curr = 1;
while(curr < n){
lg++;
curr *= 2;
}
vector<vector<bool>> v(n, vector<bool>(lg, 0));
int num = (n - lg) / 2;
if((n - lg) % 2 != 0) num++;
curr = 0;
int d = 4;
for(int k = 0; k < lg; k++){
int have = 1;
//cout << "K "<< k << " num "<< num << endl;
for(int i = lg; i < n; i++){
if(curr == num) {curr = 0; have = 1 - have;}
//cout << ""
v[i][k] = have;
curr++;
}
num = (n - lg) / d;
if((n - lg) % d != 0) num++;
d *= 2;
}
for(int i = lg; i < n; i++){
string s = "";
for(int j = 0; j < n; j++) s += '0';
s[i] = '1';
add_element(s);
for(int j = 0; j < lg; j++){
if(v[i][j]) s[j] = '1';
if(v[i][j]) add_element(s);
}
}
add_lg(n, lg);
compile_set();
vector<vector<bool>> reconstruct(n, vector<bool>(lg, 0));
vector<int> ind(lg);
vector<int> possible;
vector<bool> possible1(n, 0);
for(int i = 0; i < n; i++){
string s = "";
for(int j = 0; j < n; j++) s += '0';
s[i] = '1';
bool rep = check_element(s);
if(!rep) {possible.push_back(i); possible1[i] = 1;}
}
check_lg(n, lg, possible, ind);
for(int k = 0; k < lg; k++){
for(int i = 0; i < n; i++){
if(possible1[i]) continue;
string s = "";
for(int j = 0; j < n; j++) s += '0';
s[i] = '1';
for(int j = 0; j < k; j++){
if(reconstruct[i][j]) s[ind[j]] = '1';
}
s[ind[k]] = '1';
bool rep = check_element(s);
if(rep) reconstruct[i][k] = 1;
}
}
/*for(int i = lg; i < n; i++){
cout << "V[i] "<<i<< endl;
for(auto x : v[i]) cout << x << " ";
cout << endl;
}*/
vector<int> ans(n);
for(int i = 0; i < n; i++){
if(possible1[i]){
for(int j = 0; j < lg; j++){
if(ind[j] == i) ans[i] = j;
}
}else{
for(int j = 0; j < n; j++){
if(reconstruct[i] == v[j]) ans[i] = j;
}
}
/*cout << "Reconstruct i "<< i << endl;
for(auto x : reconstruct[i]) cout << x << " ";
cout << endl;*/
}
return ans;
}
/*
namespace helper {
set<string> set_;
bool compiled = false;
int n;
vector<int> p;
int w;
int r;
int read_int() {
int x;
cin >> x;
return x;
}
}
using namespace helper;
// A convenience function.
int get_p(int i) {
int ret = p[i];
return ret;
}
int main() {
n = read_int();
w = read_int();
r = read_int();
p = vector<int>(n);
for (int i = 0; i < n; i++) {
p[i] = read_int();
}
vector<int> answer = restore_permutation(n, w, r);
for(int x : answer) cout << x << " ";
cout << endl;
cout << "Here"<< endl;
return 0;
if (answer.size() != n) {
printf("WA\n");
return 0;
}
printf("%d", answer[0]);
for (int i = 1; i < n; i++) {
printf(" %d", answer[i]);
}
printf("\n");
return 0;
}
void wa() {
printf("WA\n");
exit(0);
}
bool check(const string& x) {
if ((int)x.length() != n) {
return false;
}
for (int i = 0; i < n; i++) {
if (x[i] != '0' && x[i] != '1') {
return false;
}
}
return true;
}
void add_element(string x) {
if (--w < 0 || compiled || !check(x)) {
wa();
}
set_.insert(x);
}
bool check_element(string x) {
if (--r < 0 || !compiled || !check(x)) {
wa();
}
return set_.count(x);
}
void compile_set() {
if (compiled) {
wa();
}
compiled = true;
set<string> compiledSet;
string compiledElement = string(n, ' ');
for (set<string>::iterator it = set_.begin(); it != set_.end(); it++) {
string s = *it;
for (int i = 0; i < n; i++) {
compiledElement[i] = s[get_p(i)];
}
compiledSet.insert(compiledElement);
}
set_ = compiledSet;
}*/
# | 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... |