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 <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cassert>
#include <tuple>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
using namespace std;
typedef long long LL;
typedef vector<int> Vi;
typedef vector<LL> VL;
typedef vector<bool> Vb;
typedef vector<vector<int>> Vii;
typedef vector<vector<vector<int>>> Viii;
typedef vector<vector<pair<int, int>>> Vip;
#define forR(i, n) for (int i = 0; i < (n); i++)
void add_element(string x);
void compile_set();
bool check_element(string x);
int N;
Vi p;
void guess(int l, int r, Vi digits) { // original [l, r] <-> digits
if (l == r) {
assert(digits.size() == 1);
p[digits[0]] = l;
return;
}
Vi left, right;
for (int t : digits) { // test t
string s(N, '0');
for (int k : digits) {
if (k != t) {
s[k] = '1';
}
}
if (check_element(s)) {
// t is in left
left.push_back(t);
} else {
right.push_back(t);
}
}
int mid = l + r >> 1;
guess(l, mid, left);
guess(mid + 1, r, right);
}
void s1(int l, int r) {
if (l == r) return;
int mid = l + r >> 1;
// label [l .. mid] seperately
for (int t = l; t <= mid; t++) {
string s(N, '0'); // 11011111 11111111
for (int k = l; k <= r; k++) {
if (k != t) {
s[k] = '1';
}
}
add_element(s);
}
s1(l, mid);
s1(mid + 1, r);
}
Vi restore_permutation(int n, int w, int r) {
N = n;
p = Vi(n, -1);
//for (int i = 1; i <= n; i++) {
// // i*0 + (n-i)*1
// string s;
// s.append(i, '0');
// s.append(n - i, '1');
// add_element(s);
//}
//compile_set();
//string cur; cur.append(n, '1');
//Vi p(n);
//for (int i = 0; i < n; i++) {
// forR(j, n) {
// if (cur[j] == '0') continue;
// cur[j] = '0';
// if (check_element(cur)) {
// // j-th is original i
// p[j] = i;
// break;
// }
// cur[j] = '1';
// }
//}
s1(0, n - 1);
compile_set();
Vi seq; forR(i, n) seq.push_back(i);
guess(0, n - 1, seq);
return p;
}
#ifdef TEST_LOCAL
Vi _p { 2,1,3,0 };
set<string> _comp;
void add_element(string x) {
string y;
for (int d : _p) {
y.append(1, x[d]);
}
_comp.insert(y);
}
void compile_set() {}
bool check_element(string x) {
return _comp.count(x) > 0;
}
int main() {
auto r = restore_permutation(4, 16, 16);
return 0;
}
#endif
Compilation message (stderr)
messy.cpp: In function 'void guess(int, int, Vi)':
messy.cpp:60:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | int mid = l + r >> 1;
| ~~^~~
messy.cpp: In function 'void s1(int, int)':
messy.cpp:67:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | int mid = l + r >> 1;
| ~~^~~
# | 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... |