题解:P15275 [IOI 2016] Unscrambling a Messy Bug 解读 Bug
SClan_Official · · 题解
先看部分分。以下
- Subtask 1:
2^n ,给暴力用的。 - Subtask 2:
w 是2n\log n ,r 是n^2 。 - Subtask 3:
w 是n^2 ,r 是2n\log n 。 - Subtask 4:
w 是2n\log n ,r 是2n\log n 。 - Subtask 5:
w 是n\log n ,r 是n\log n 。
出现
考虑分治。
我们先不考虑必须要先插入后查询的问题,假设可以边插入边查询。
那么我们就考虑如何把“当前区间对应的数字的集合
这个并不是很困难,因为左半部分和有半部分我们是知道的。所以我们就可以,对于所有处于左半部分的数字
现在只剩下一个问题了:实际上要求先插入后查询。这个也不是很困难,只要我们能够把某个区间内的
首先我们要能够区分大小不同的区间。我们想到把所有区间之外的元素都置为
插入和查询次数不变。此题得解。
另外,如果你能够做到
:::info[rec&code] rec。
#include<vector>
#include<string>
extern "C"
{
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);
}
#include <vector>
#include <algorithm>
using namespace std;
static auto U = [](auto x) { return [x](auto ...y) { return x(x, y...); }; };
vector<int> restore_permutation(int n, int, int)
{
U([&](auto &&self, int l, int r) -> void
{
if(l == r) return;
for(int i=l;i<=(l+r)/2;i++)
{
string str(n, '1');
for(int j=l;j<=r;j++) str[j] = '0';
str[i] = '1';
add_element(str);
}
self(self, l, (l + r) / 2);
self(self, (l + r) / 2 + 1, r);
})(0, n-1);
compile_set();
vector<int> vt;
for(int i=0;i<n;i++) vt.push_back(i);
vector<int> ans(n, 0);
U([&](auto &&self, int l, int r, vector<int> may) -> void
{
// printf("l = %d, r = %d, may = ", l, r);
// for(int x : may) printf("%d ", x);
// printf("\n"); fflush(stdout);
if(l == r)
{
ans[may[0]] = l;
return;
}
vector<int> alpha, beta;
for(int i : may)
{
string str(n, '1');
for(int j : may) str[j] = '0';
str[i] = '1';
if(check_element(str)) alpha.push_back(i);
else beta.push_back(i);
}
self(self, l, (l + r) / 2, alpha);
self(self, (l + r) / 2 + 1, r, beta);
})(0, n-1, vt);
return ans;
}
:::