转成切比雪夫距离后容易发现有解的充要条件是所有 (x+y) 奇偶性一样。
观察到 (mle 40),大概是 (log(x+y)) 的范围,于是可以考虑二进制拆分。
结论:(1,2,4,dots,2^k) 可以拼出所有 (|x|+|y|le 2^{k+1}-1) 且 (x+yequiv 1pmod 2) 的坐标。可以用递归证明。
那么直接构造答案,如果 (x+y) 为偶数就在集合中多加一个 (1)。
从大到小枚举集合中的每个数,每次选择 (x),(y) 中绝对值较大的一个操作。
代码:
#include <bits/stdc++.h>
#define DC int T = gi <int> (); while (T--)
#define DEBUG fprintf(stderr, "Passing [%s] line %d
", __FUNCTION__, __LINE__)
#define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int, int> PII;
typedef pair <LL, LL> PLL;
template <typename T>
inline T gi()
{
T x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int N = 1003, M = N << 1;
int n;
LL x[N], y[N];
bool fl1, fl2;
LL pw2[33];
template <typename T>
inline T mabs(T x) {return x < 0 ? -x : x;}
int main()
{
//freopen(".in", "r", stdin); freopen(".out", "w", stdout);
n = gi <int> ();
for (int i = 1; i <= n; i+=1) x[i] = gi <LL> (), y[i] = gi <LL> (), fl1 |= ((x[i] + y[i]) % 2 == 0), fl2 |= ((x[i] + y[i]) & 1);
if (fl1 && fl2) return puts("-1"), 0;
for (int i = (pw2[0] = 1); i <= 31; i+=1) pw2[i] = 2ll * pw2[i - 1];
int mx = 31;
if (fl1)
{
++mx;
for (int i = 32; i >= 1; i-=1) pw2[i] = pw2[i - 1];
pw2[0] = 1;
puts("33");
for (int i = 32; ~i; i-=1) printf("%lld ", pw2[i]);
}
else
{
puts("32");
for (int i = 31; ~i; i-=1) printf("%lld ", pw2[i]);
}
puts("");
for (int i = 1; i <= n; i+=1)
{
for (int j = mx; ~j; j-=1)
if (mabs(x[i]) > mabs(y[i]))
{
if (x[i] < 0) x[i] += pw2[j], cout << 'L';
else x[i] -= pw2[j], cout << 'R';
}
else
{
if (y[i] < 0) y[i] += pw2[j], cout << 'D';
else y[i] -= pw2[j], cout << 'U';
}
puts("");
}
return !!0;
}
启发:
- 构造题观察到数据范围为 (log) 级 ( ightarrow) 二进制拆分。