题解:P17040 [NWERC 2021] 泰森环 / Dyson Circle
Fine_Dust_Z · · 题解
神人题。
观察这张图。
我们发现这样是一种最小花费,我们定义:
那么就有答案:
。
那么要是他的点发生了变化了呢?像是这样:
我们可以将他转化一下。
我去这不是个正方形吗,那么我们直接将曼哈顿距离转切比雪夫距离(不会的戳这里),我们将切比雪夫距离带入原来的曼哈顿距离:
现在只是求这个矩形的周长了,我们知道矩形的长
则矩形的周长为:
这个就是我们的答案啦?
:::warning[注意] 如果所有点都在一条对角线上,那么我们构造的矩形就会让里面的部分不是四联通,因此需要多花一个格子去构造四联通。 :::
:::success[code]
#include <bits/stdc++.h>
#define int long long
using namespace std;
template<typename T>inline void read(T&x) {
x = 0;
char c;
int sgin = 1;
do {
c = getchar();
if (c == '-')sgin = -1;
} while (!isdigit(c));
do {
x = (x << 3) + (x << 1) + (c & 15);
c = getchar();
} while (isdigit(c));
x *= sgin;
}
void write(int x) {
static int sta[40];
int top = 0;
do {
sta[top++] = x % 10;
x /= 10;
} while (x);
while (top) putchar('0' + sta[--top]);
putchar('\n');
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
read(n);
int x_min = LLONG_MAX, x_max = LLONG_MIN;
int y_min = LLONG_MAX, y_max = LLONG_MIN;
for (int i = 0; i < n; i++) {
int x, y;
read(x);read(y);
x_min = min(x_min, x + y);
x_max = max(x_max, x + y);
y_min = min(y_min, x - y);
y_max = max(y_max, x - y);
}
int x = x_max - x_min;
int y = y_max - y_min;
int ans = 4 + x + y;
if (n > 1 && (x == 0 || y == 0)) {
ans++;
}
write(ans);
return 0;
}
:::