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 "messy.h"
#include <string>
#include <vector>
using namespace std;
/*
Let n = 8
First, we can insert
10000000 all i such that p[i] <= n/2
01000000
00100000
00010000
Now, we can query
10000000
...
00000001
where 1 is only at position i
The positions i where the query is true, satisfy the following:
p_inv[i] <= n/2
10001111
01001111
11111000
11110100
10111111
11101111
11111011
11111110
From these, we can identify all i such that (p[i] <= n/2).
Let later sequence = b and input sequence = a
a[i] == 1
b[j] == 1
j = p[i]
We identify p[i]
X[i] <= p[i] <= Y[i]
for X = n, n/2 ... 2
We partition the set {i} into two groups, those with p_inv[i] % n <= n/2,
and those with p_inv[i] % n > n/2
*/
int N;
vector<int> p_inv(128, 0);
void find_p_inv(int a, int b, vector<int> goodcells)
//goodcells contains all the elements with p_inv in the range a..b
{
if(a == b)
{
p_inv[goodcells[0]] = a;
return;
}
string base_query(N, '1');
for(int g: goodcells)
base_query[g] = '0';
vector<int> goodcells_1, goodcells_2;
for(int g: goodcells)
{
string query = base_query;
query[g] = '1';
if(check_element(query))
goodcells_1.push_back(g);
else
goodcells_2.push_back(g);
}
find_p_inv(a, (a+b)/2, goodcells_1);
find_p_inv((a+b)/2+1, b, goodcells_2);
}
vector<int> restore_permutation(int n, int w, int r)
{
N = n;
for(int g = n; g >= 2; g /= 2) //size of gap
{
for(int i = 0; i < n; i += g) //starting point of gap
{
for(int o = 0; o < g/2; o++)
{
string s(n, '1');
for(int j = i; j < i+g; j++)
if(j-i != o)
s[j] = '0';
add_element(s);
}
}
}
compile_set();
// cerr << n << '\n';
vector<int> goodcells;
for(int i = 0; i < n; i++) goodcells.push_back(i);
find_p_inv(0, n-1, goodcells);
vector<int> p(n);
for(int i = 0; i < n; i++)
p[p_inv[i]] = i;
return p;
}
# | 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... |