이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <vector>
#include <string>
#include <unordered_set>
#include "messy.h"
int N, per[130];
void insertStrings(int st, int dr) {
if(st == dr) {
return ;
}
int mid = (st + dr) >> 1;
for(int b = st; b <= mid; b++) {
std::string s = "";
for(int j = 1; j <= N; j++) {
if(j < st) {
s += '1';
} else if(j > dr) {
s += '1';
} else if(j == b) {
s += '1';
} else {
s += '0';
}
}
add_element(s);
}
insertStrings(st, mid);
insertStrings(mid + 1, dr);
}
void divide(int st, int dr, std::unordered_set<int> emptyPositions, std::unordered_set<int> knownPos) {
if(st == dr) {
//std::cout << st << " : " << *emptyPositions.begin() << '\n';
per[st - 1] = *emptyPositions.begin() - 1;
return ;
}
int mid = (st + dr) >> 1;
std::unordered_set<int> leftHalf, rightHalf;
for(int b : emptyPositions) {
std::string s = "";
for(int j = 1; j <= N; j++) {
if(knownPos.find(j) != knownPos.end()) {
s += '1';
} else if(j == b) {
s += '1';
} else {
s += '0';
}
}
bool r = check_element(s);
if(r == true) {
leftHalf.insert(b);
}
}
for(int b : emptyPositions) {
if(leftHalf.find(b) == leftHalf.end()) {
rightHalf.insert(b);
}
}
///recurse to the left half
std::unordered_set<int> newEmptyPos, newKnownPos;
for(int b : emptyPositions) {
if(rightHalf.find(b) == rightHalf.end()) {
newEmptyPos.insert(b);
} else {
newKnownPos.insert(b);
}
}
for(int b : knownPos) {
newKnownPos.insert(b);
}
divide(st, mid, newEmptyPos, newKnownPos);
///recurse to the right half
newEmptyPos.clear();
newKnownPos.clear();
for(int b : emptyPositions) {
if(leftHalf.find(b) == rightHalf.end()) {
newEmptyPos.insert(b);
} else {
newKnownPos.insert(b);
}
}
for(int b : knownPos) {
newKnownPos.insert(b);
}
divide(mid + 1, dr, newEmptyPos, newKnownPos);
}
std::vector<int> restore_permutation(int n, int w, int r) {
/*
add_element("0");
compile_set();
check_element("0");
return std::vector<int>();
*/
N = n;
///Insert all strings
insertStrings(1, N);
///Compile
compile_set();
///Retrieve permutation
std::unordered_set<int> emptyPositions;
for(int i = 1; i <= N; i++) {
emptyPositions.insert(i);
}
std::unordered_set<int> knownPos;
divide(1, N, emptyPositions, knownPos);
///Ansert
std::vector<int> sol(N);
for(int i = 0; i < N; i++) {
sol[i] = per[i];
}
return sol;
}
# | 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... |