이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifndef LOCAL
#include "messy.h"
#else
#include <vector>
#include <cstdio>
#include <string>
#include <set>
#include <cstdlib>
#include <iostream>
void add_element(std::string x);
bool check_element(std::string x);
void compile_set();
std::vector<int> restore_permutation(int n, int w, int r);
namespace helper {
std::set<std::string> set_;
bool compiled = false;
int n;
std::vector<int> p;
int w;
int r;
int read_int() {
int x;
std::cin >> x;
return x;
}
}
int get_p(int i) {
using namespace helper;
int ret = p[i];
return ret;
}
int main() {
using namespace helper;
n = read_int();
w = read_int();
r = read_int();
p = std::vector<int>(n);
for (int i = 0; i < n; i++) {
p[i] = read_int();
}
std::vector<int> answer = restore_permutation(n, w, r);
if ((int) 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 std::string& x) {
using namespace helper;
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(std::string x) {
using namespace helper;
if (--w < 0 || compiled || !check(x)) {
wa();
}
set_.insert(x);
}
bool check_element(std::string x) {
using namespace helper;
if (--r < 0 || !compiled || !check(x)) {
wa();
}
return set_.count(x);
}
void compile_set() {
using namespace helper;
if (compiled) {
wa();
}
compiled = true;
std::set<std::string> compiledSet;
std::string compiledElement = std::string(n, ' ');
for (std::set<std::string>::iterator it = set_.begin(); it != set_.end(); it++) {
std::string s = *it;
for (int i = 0; i < n; i++) {
compiledElement[i] = s[get_p(i)];
}
compiledSet.insert(compiledElement);
}
set_ = compiledSet;
}
#endif
std::vector<int> restore_permutation(int n, int w, int r) {
std::vector<int> p(n);
std::string s(n, '0');
for (int i = 0; i + 1 < n; i++) {
p.back() ^= n - i - 1;
s[i] = '1';
add_element(s);
}
compile_set();
std::vector<int> found(n);
s = std::string(n, '0');
for (int i = 0; i + 1 < n; i++) {
for (int j = 0; j < n; j++) {
if (found[j] == 0) {
s[j] = '1';
int res = check_element(s);
if (res) {
p[i] = j;
found[j] = 1;
p.back() ^= j;
break;
}
s[j] = '0';
}
}
}
std::vector<int> res(n);
for (int i = 0; i < n; i++) {
res[p[i]] = i;
}
return res;
}
# | 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... |