APIO 2022 Practice
MACHINE
题解
由于只能询问一次,并且询问的值的范围为
对于
对于
为了实现方便代码直接枚举
代码
#include <bits/stdc++.h>
#include "machine.h"
std::vector<int> guess_permutation (int n)
{
int x = -1, k = log2(n);
std::vector<int> a; a.resize(n);
for (int i = 0; i < n; i++) a[i] = i;
if (!(n & 1))
{
a.clear();
for (int i = 0; i <= n + 2; i++)
if (i != (1 << k) - 2 and i != (1 << k) - 1 and i != (1 << k) + 1) a.push_back(i);
}
std::vector<int> b = use_machine(a);
for (int y = 0; y <= 255; y++)
{
std::vector<int> c = b;
for (int i = 0; i < n; i++) c[i] ^= y;
std::sort(c.begin(), c.end());
if (c == a) {x = y; break;}
}
for (int i = 0; i < n; i++) b[i] ^= x;
std::vector<int> p; p.resize(n);
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (a[i] == b[j]) p[j] = i;
return p;
}
PYRAMIDS
题解
直接判断和是否相等即可,可以前缀和。
代码
#include <bits/stdc++.h>
#include "pyramids.h"
int n; std::vector<long long> A, B;
void init (std::vector<int> a, std::vector<int> b)
{
n = a.size(); A.resize(n); B.resize(n);
for (int i = 0; i < n; i++) A[i] = a[i], B[i] = b[i];
for (int i = 1; i < n; i++) A[i] += A[i - 1], B[i] += B[i - 1];
}
inline long long sumA (int l, int r) {return A[r] - (l ? A[l - 1] : 0);}
inline long long sumB (int l, int r) {return B[r] - (l ? B[l - 1] : 0);}
bool can_convert (int l1, int r1, int l2, int r2) {return sumA(l1, r1) == sumB(l2, r2);}
TREASURE
题解
考虑如何压缩信息,由于不要求有顺序,因此可以考虑离散化。
首先
接下来只能传输
不妨设
代码
#include <bits/stdc++.h>
#include "treasure.h"
std::vector<int> encode (std::vector<int> x, std::vector<int> y)
{
std::vector<int> e; int n = x.size();
for (int i = 0; i < n; i++) e.push_back(x[i] + 1000000000);
for (int i = 0; i < n; i++) e.push_back(y[i] + 1500000000);
std::vector<std::pair<int, int>> p; p.resize(n);
for (int i = 0; i < n; i++) p[i] = {x[i], y[i]};
std::sort(y.begin(), y.end());
y.resize(std::unique(y.begin(), y.end()) - y.begin());
std::sort(p.begin(), p.end());
std::vector<int> t; t.resize(n + 1);
auto add = [&] (int x) {for (int i = x; i <= n; i += i & (-i)) t[i]++;};
auto ask = [&] (int x) {int res = 0; for (int i = x; i; i -= i & (-i)) res += t[i]; return res;};
for (int i = 0; i < n; i++)
{
int j = std::lower_bound(y.begin(), y.end(), p[i].second) - y.begin() + 1;
int w = ask(j); add(j); e.push_back(i * (i + 1) / 2 + w);
}
return e;
}
std::vector<int> decode (std::vector<int> e)
{
int n = (int)e.size() / 3;
std::sort(e.begin(), e.end());
std::vector<int> x, y; x.resize(n); y.resize(n);
std::vector<int> ans;
std::vector<int> t; t.resize(n + 1);
auto add = [&] (int x, int w) {for (int i = x; i <= n; i += i & (-i)) t[i] += w;};
auto ask = [&] (int x) {int res = 0; for (int i = x; i; i -= i & (-i)) res += t[i]; return res;};
for (int i = 0; i < n; i++) x[i] = e[i + n] - 1000000000, y[i] = e[i + n + n] - 1500000000;
for (int i = 1; i <= n; i++) add(i, 1);
for (int i = n - 1; ~i; i--)
{
int w = e[i] - i * (i + 1) / 2;
int ql = 1, qr = n;
while (ql < qr)
{
int mid = (ql + qr) >> 1;
ask(mid) > w ? qr = mid : ql = mid + 1;
}
ans.push_back(x[i]); ans.push_back(y[ql - 1]); add(ql, -1);
}
return ans;
}