题意
有 $n$ 个不大于 $m$ 的自然数 $a_i$, 你需要找到一个不大于 $k$ 的自然数 $s$,以最小化:
$$\min\limits_{i \neq j} \left(a_i + s\right) \oplus \left(a_j + s\right)$$
你只需要输出这个最小值即可。其中 $\oplus$ 表示按位异或。
$2 \leq n \leq 10^6, 0 \leq m, k \leq 10^8$
题解
首先注意到异或的几个性质:
- 对于 $i < j < k$,$i \oplus k \geq \min\left\{i \oplus j, j \oplus k\right\}$
- $i \oplus j \geq \left\lvert i - j \right\rvert$
- $\exist k < 2 \left\lvert i - j \right\rvert, \left(i + k\right) \oplus \left(j + k\right) = \left\lvert i - j \right\rvert$
根据性质 1,我们把 $a$ 排序,求 $\displaystyle\min\limits_{i = 1}^{n - 1} \left(a_i + s\right) \oplus \left(a_{i + 1} + s\right)$ 即可。
我们记 $\left\lvert a_i - a_{i + 1}\right\rvert$ 的最小值是 $t$。若 $k \geq 2t$,那么由结论 2 和结论 3 可得答案就是 $t$。否则直接用 $\Theta\left(nk\right)$ 暴力即可。注意到 $t \leq \dfrac m{n - 1}$,所以复杂度是 $\mathcal O\left(m\right)$ 的。
总复杂度:$\mathcal O\left(n \log n + m \right)$
// Please submit with C++17! It's best to use C++20 or higher version.
// By Koicy (https://koicy.ly)
// rbtree (i@koicy.ly)
// This is my kingdom code.
// #define EFILE ""
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define _CONSOLE 0
#define _MULTI_TESTS 0
using namespace std;
using tp = long long;
constexpr tp ZERO = 0, ONE = 1, INF32 = -1u >> 2, INF = -1ull >> 2;
// :\
namespace _IO {
constexpr size_t BUF = 217217;
unsigned long long h = 5;
char ibuf[BUF], obuf[BUF];
char *li, *ri, *lo = obuf, *ro = obuf + BUF, st[100], *tp = st;
FILE *Istream = stdin, *Ostream = stdout;
char GC() {
#if _CONSOLE
return getchar();
#endif
if (li == ri) {
li = ibuf; ri = li + fread(li, 1, BUF, Istream);
if (li == ri) return '\n';
}
return *li++;
}
void _flush() {
fwrite(obuf, 1, lo - obuf, Ostream);
lo = obuf;
}
void PC(char ch) {
#if _CONSOLE
return (void)putchar(ch);
#endif
if (lo == ro) _flush();
*lo++ = ch;
}
template <typename Type>
void r_int(Type& x) {
bool neg = 0;
char ch = GC();
while (ch < 48 || ch > 57) { neg ^= ch == 45; ch = GC(); }
for (x = 0; ch >= '0' && ch <= '9'; ch = GC()) x = x * 10 + (ch & 15);
if (neg) x = -x;
}
template <typename Type>
void r_uint(Type& x) {
char ch = GC();
while (ch < 48 || ch > 57) ch = GC();
for (x = 0; ch >= '0' && ch <= '9'; ch = GC()) x = x * 10 + (ch & 15);
}
template <typename Type>
void r_db(Type& x) {
bool neg = 0;
char ch = GC();
while (ch < 48 || ch > 57) { neg ^= ch == 45; ch = GC(); }
for (x = 0; ch >= '0' && ch <= '9'; ch = GC()) x = x * 10 + (ch & 15);
if (ch == '.') {
double p = 1;
for (ch = GC(); ch >= '0' && ch <= '9'; ch = GC()) x = x + (p /= 10) * (ch & 15);
}
if (neg) x = -x;
}
template <typename Type>
void w_uint(Type x) {
if (!x) { PC('0'); return; }
while (x) { *tp++ = x % 10; x /= 10; }
while (tp > st) PC(*--tp ^ 48);
}
template <typename Type>
void w_int(Type x) {
if (x < 0) { PC('-'); w_uint(-x); }
else w_uint(x);
}
template <typename Type>
void w_db(Type x) {
double l = floor(abs(x));
string s;
if (x < 0) { PC('-'); x = -x; }
x -= l;
if (!l) PC(48);
while (l) { s.push_back(l - floor(l / 10) * 10); l = floor(l / 10); }
for (auto i = s.rbegin(); i != s.rend(); ++i) PC(*i ^ 48);
PC(46);
for (unsigned long long f = 0; f < h; ++f) {
x *= 10;
PC(floor(x - floor(x / 10) * 10) + 48);
}
}
struct IO {
IO& operator>>(char& x) { do { x = GC(); } while (x == 32 || x == 10 || x == 13); return *this; }
IO& operator>>(string& x) {
char c;
operator>>(c);
x = c;
for (c = GC(); c != 32 && c != 10 && c != 13; c = GC()) x.push_back(c);
x.shrink_to_fit();
return *this;
}
IO& operator>>(short& x) { r_int(x); return *this; }
IO& operator>>(int& x) { r_int(x); return *this; }
IO& operator>>(long& x) { r_int(x); return *this; }
IO& operator>>(long long& x) { r_int(x); return *this; }
IO& operator>>(unsigned short& x) { r_uint(x); return *this; }
IO& operator>>(unsigned int& x) { r_uint(x); return *this; }
IO& operator>>(unsigned long& x) { r_uint(x); return *this; }
IO& operator>>(unsigned long long& x) { r_uint(x); return *this; }
IO& operator>>(float& x) { r_db(x); return *this; }
IO& operator>>(double& x) { r_db(x); return *this; }
IO& operator<<(short x) { w_int(x); return *this; }
IO& operator<<(int x) { w_int(x); return *this; }
IO& operator<<(long x) { w_int(x); return *this; }
IO& operator<<(long long x) { w_int(x); return *this; }
IO& operator<<(unsigned short x) { w_uint(x); return *this; }
IO& operator<<(unsigned int x) { w_uint(x); return *this; }
IO& operator<<(unsigned long x) { w_uint(x); return *this; }
IO& operator<<(unsigned long long x) { w_uint(x); return *this; }
IO& operator<<(char x) { PC(x); return *this; }
IO& operator<<(const string& x) { for (auto& i : x) PC(i); return *this; }
IO& operator<<(float& x) { w_db(x); return *this; }
IO& operator<<(double& x) { w_db(x); return *this; }
void flush() { _flush(); }
void precision(unsigned long long p) { h = p; }
IO() {
#ifdef EFILE
Istream = fopen(EFILE ".in", "r");
Ostream = fopen(EFILE ".out", "w");
#else
#ifdef _LOCAL
Istream = fopen("input.txt", "r");
#endif
#endif
}
~IO() { _flush(); }
} bin;
}
using _IO::bin;
// :/
tp n, m, k, t = INF;
vector<tp> a;
void STRUGGLING([[maybe_unused]] tp TEST_NUMBER) {
bin >> n >> m >> k;
a.resize(n);
for (auto& i : a) bin >> i;
stable_sort(a.begin(), a.end());
for (tp i = 1; i < n; ++i) t = min(t, abs(a[i] - a[i - 1]));
if (k >= 2 * t) { bin << t << '\n'; return; }
t = INF;
for (tp s = 0; s <= k; ++s) {
for (tp i = 1; i < n; ++i) t = min(t, (a[i] + s) ^ (a[i - 1] + s));
}
bin << t << '\n';
}
void MIST() {
}
#include <fstream>
signed main() {
tp t = 0, _t = 1;
MIST();
#if _MULTI_TESTS
bin >> _t;
#endif
while (t < _t) STRUGGLING(++t);
return 0;
}
//*/