# bzoj 1052 [HAOI2007]覆盖问题 二分答案+贪心

### 代码

#include <bits/stdc++.h>
#define inf 1e9
#define N 20010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
x = 0; int f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
int x, y;
} a[N], b[N], c[N];
int n, tot;
void del(int x1, int x2, int y1, int y2) {
int tmp = 0;
for (int i = 1; i <= tot; i++)
if (x1 > b[i].x || b[i].x > x2 || y1 > b[i].y || b[i].y > y2)
c[++tmp] = b[i];
tot = tmp;
for (int i = 1; i <= tmp; i++) b[i] = c[i];
}
void solve(int key, int mid) {
int x1 = inf, y1 = inf, x2 = -inf, y2 = -inf;
for (int i = 1; i <= tot; i++) {
chkmin(x1, b[i].x), chkmax(x2, b[i].x);
chkmin(y1, b[i].y), chkmax(y2, b[i].y);
}
if (key == 1) del(x1, x1 + mid, y1, y1 + mid);
if (key == 2) del(x2 - mid, x2, y1, y1 + mid);
if (key == 3) del(x1, x1 + mid, y2 - mid, y2);
if (key == 4) del(x2 - mid, x2, y2 - mid, y2);
}
bool check(int mid) {
for (int x = 1; x <= 4; x++)
for (int y = 1; y <= 4; y++) {
for (int i = 1; i <= n; i++) b[i] = a[i];
tot = n, solve(x, mid), solve(y, mid);
int x1 = inf, y1 = inf, x2 = -inf, y2 = -inf;
for (int i = 1; i <= tot; i++) {
chkmin(x1, b[i].x), chkmax(x2, b[i].x);
chkmin(y1, b[i].y), chkmax(y2, b[i].y);
}
if (x2 - x1 <= mid && y2 - y1 <= mid) return true;
}
return false;
}
int main() {
for (int i = 1; i <= n; i++)