[Solution] [PA 2017] 马赛克
\text{Task}
题目链接:[PA 2017] 马赛克
给定
-
- 任意两个正方形不交。
\text{Solution}
首先,由于所有正方形以给定点为左下角构造,则最终形成的矩形的左下角一定在
如果不存在这个点,则直接输出 NIE。
钦定一个构造正方形的顺序:优先构造
但只有点
可以考虑找到一个矩形的上边界
接下来要做的就是快速枚举
显然
此时讨论两种情况:
- 情况一:
q 构造出的正方形边长直接贯穿了整个矩形的宽度(即抵达了最终可以构造出的矩形的右边界),此时对于其他点而言,上边界的限制即为y_q 。 - 情况二:若
q 所构造的正方形边长小于整个矩形的宽度,此时q 对应正方形的右边界即为l_q=R-y_q ,则要铺满整个矩阵,必然需要存在一个正方形在右侧紧接q ,即存在点i 满足x_i=l_q ,故而R=y_q+x_i 。
因此
填充矩阵时,当前每个点均有了边界的限制,按照前文所说的顺序依次对
这个贪心的正确性是显然的,若不填充为最大正方形,留下的缝隙在当前顺序下是没有点可以填充的。
\text{Code}
通过适时的剪枝可以顶着 std::vector 的巨大常数拿下最优解。实则因为没人做。
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0); std::cout.tie(0);
typedef long long i64;
constexpr int INF = 0x3f3f3f3f;
int T; std::cin >> T;
auto NIE = []() { return (void)printf("NIE\n"); };
auto Solve = [&](int test_case = 0) {
int n; std::cin >> n;
std::vector<std::pair<std::pair<i64, i64>, int>> point(n);
i64 MinX = INF, MinY = INF;
for (int i = 0; i < n; i++) {
std::cin >> point[i].first.first >> point[i].first.second;
point[i].second = i;
MinX = std::min(MinX, point[i].first.first);
MinY = std::min(MinY, point[i].first.second);
}
for (int i = 0; i < n; i++) {
point[i].first.first -= MinX;
point[i].first.second -= MinY;
}
sort(point.begin(), point.end());
if (point[0].first != std::make_pair(0LL, 0LL)) return NIE();
if (n == 1) return (void)printf("TAK 1\n");
std::vector<i64> rst(n);
int idx = 0;
for (int i = 0; i < n; i++)
if (point[i].first.first == point[0].first.first) idx = i;
i64 R = point[idx].first.second, area = 0, MaxX = 0, MaxY = 0;
bool flg = true;
auto solve = [&](i64 R, int f = 0) {
area = MaxX = MaxY = 0;
flg = true;
for (int i = 0; i < n; i++) {
if (f && i == idx) continue;
i64 d = R-point[i].first.second;
for (int j = 0; j < i; j++) {
if (f && j == idx) continue;
if (point[j].first.second <= point[i].first.second && \
point[j].first.first+rst[point[j].second] > point[i].first.first && \
point[j].first.second+rst[point[j].second] > point[i].first.second) {
flg = false; break;
}
}
if (!flg) break;
for (int j = i+1; j < n; j++) {
if (f && j == idx) continue;
if (point[i].first.first != point[j].first.first) break;
if (point[i].first.first <= point[j].first.first && \
point[i].first.second <= point[j].first.second) {
d = std::min(d, std::max(point[j].first.first-point[i].first.first, point[j].first.second-point[i].first.second));
}
}
for (int j = 0; j < i; j++) {
if (f && j == idx) continue;
i64 tmp = point[j].first.first;
point[j].first.first = std::min(tmp+rst[point[j].second]-1, point[i].first.first);
if (point[i].first.first <= point[j].first.first && \
point[i].first.second <= point[j].first.second) {
d = std::min(d, std::max(point[j].first.first-point[i].first.first, point[j].first.second-point[i].first.second));
}
point[j].first.first = tmp;
}
if (d <= 0) { flg = false; break; }
rst[point[i].second] = d;
MaxX = std::max(MaxX, point[i].first.first+d);
MaxY = std::max(MaxY, point[i].first.second+d);
area += d*d;
}
};
solve(R, 1);
if (flg && R*MaxX == area) {
printf("TAK ");
rst[point[idx].second] = MaxX;
for (int i = 0; i < n; i++) printf("%lld ", rst[i]);
return (void)printf("\n");
}
if (idx == n-1) return NIE();
std::vector<int> candidat;
for (int i = idx+1; i < n; i++) {
R = point[idx].first.second+point[i].first.first-point[idx].first.first;
flg = true;
if (R-point[i].first.second > 0 && R > point[idx].first.second && R > point[i].first.second) {
for (int j = 0; j < n; j++) {
if (j == i || j == idx) continue;
if (point[j].first.second >= R) { flg = false; break; }
if (point[idx].first.first <= point[j].first.first && \
point[j].first.first < point[i].first.first && \
point[j].first.second >= point[idx].first.second)
{ flg = false; break; }
if (point[i].first.first <= point[j].first.first && \
point[j].first.first < point[i].first.first+R-point[i].first.second && \
point[i].first.second <= point[j].first.second)
{ flg = false; break; }
}
if (flg) candidat.emplace_back(R);
}
}
sort(candidat.begin(), candidat.end());
candidat.erase(unique(candidat.begin(), candidat.end()), candidat.end());
for (i64 R : candidat) {
solve(R, 0);
if (flg && R*MaxX == area) {
printf("TAK ");
for (int i = 0; i < n; i++) printf("%lld ", rst[i]);
return (void)printf("\n");
}
}
return NIE();
};
for (int i = 1; i <= T; i++) Solve(i);
return 0;
}