跳到正文

题解

题解:P12054 [THUPC 2025 决赛] 食堂

hwq202314约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年10月3日
收录
2026年10月16日
更新
2026年11月16日
篇幅
约 6 分钟
获赞
1
题目
P12054

版本

  1. v1当前
笑点解析

本人在模拟赛里因为内存超了 33 MB 爆蛋了。(以后一定要在赛时算内存啊)

废话少说,直接讲核心。

题目说了一共有三种人,按照这三种人的要求开三个优先队列,存储符合要求的空座位,并按照距离从小到大排序。

这道题距离还不一般,经过手玩可以发现除了桌子右上角的座位要额外增加 22 以外,其他三个座位都是距离原点的曼哈顿距离。

我赛时以为空间允许直接预处理把 10000×1000010000 \times 10000 个点全都塞进优先队列里,赛后发现这样做爆空间了。实际上不需要这么多点,因为查询 q≤5×105q \le 5 \times 10^5,所以我们只需要预处理略大于 qq 个座位即可,经过计算发现预处理到 (3000,3000)(3000,3000) 就完全足够了。

那这样剩下的部分就很简单了。用一个二维数组标记每个座位是否有人。

  • 对于操作一只要取出对应的优先队列的第一个满足条件的座位。
  • 对于操作二,如果是离开座位就把这个座位所在的四个座位全都塞进优先队列里供下次使用;如果这个座位来了个人就标记成有人就行了。
AC 代码
C++
#include <bits/stdc++.h>

namespace Qwarth {

const int N = 10005;

bool vis[N][N];

struct nn {
	int x, y;
	bool operator<(const nn &o) const {
		int s = x + y, os = o.x + o.y;
		if (x % 3 == 2 && y % 3 == 2) {
			s += 2;
		}
		if (o.x % 3 == 2 && o.y % 3 == 2) {
			os += 2;
		}
		if (s != os) {
			return s > os;
		} else if (x != o.x) {
			return x > o.x;
		} else {
			return y > o.y;
		}
	}
} t;

std::priority_queue< nn > q0, q1, q2;

void main() {
	int Q;
	std::cin >> Q;
	for (int i = 1; i <= 3000; ++i) {
		if ((i % 3) == 0) {
			continue;
		}
		for (int j = 1; j <= 3000; ++j) {
			if ((j % 3) == 0) {
				continue;
			}
			q0.push({ i, j });
			q1.push({ i, j });
			q2.push({ i, j });
		}
	}
	while (Q--) {
		int op;
		std::cin >> op;
		if (op == 1) {
			int o, _x = 0, _y = 0;
			std::cin >> o;
			if (o == 0) {
				while (!q0.empty()) {
					t = q0.top();
					q0.pop();
					_x = (t.x % 3 == 1) ? 1 : -1;
					_y = (t.y % 3 == 1) ? 1 : -1;
					if (!vis[t.x][t.y] && !vis[t.x + _x][t.y] && !vis[t.x][t.y + _y] && !vis[t.x + _x][t.y + _y]) {
						break;
					}
				}
			} else if (o == 1) {
				while (!q1.empty()) {
					t = q1.top();
					q1.pop();
					_x = (t.x % 3 == 1) ? 1 : -1;
					_y = (t.y % 3 == 1) ? 1 : -1;
					if (!vis[t.x][t.y] && !vis[t.x + _x][t.y] && !vis[t.x][t.y + _y]) {
						break;
					}
				}
			} else {
				while (!q2.empty()) {
					t = q2.top();
					q2.pop();
					_x = (t.x % 3 == 1) ? 1 : -1;
					_y = (t.y % 3 == 1) ? 1 : -1;
					if (!vis[t.x][t.y]) {
						break;
					}
				}
			}
			vis[t.x][t.y] = 1;
			std::cout << t.x << ' ' << t.y << '\n';
		} else {
			int x, y, _x, _y;
			std::cin >> x >> y;
			_x = (x % 3 == 1) ? 1 : -1;
			_y = (y % 3 == 1) ? 1 : -1;
			if (vis[x][y]) {
				vis[x][y] = 0;
				q2.push({ x, y });
				q2.push({ x, y + _y });
				q2.push({ x + _x, y });
				q2.push({ x + _x, y + _y });

				q1.push({ x, y });
				q1.push({ x, y + _y });
				q1.push({ x + _x, y });
				q1.push({ x + _x, y + _y });

				q0.push({ x, y });
				q0.push({ x, y + _y });
				q0.push({ x + _x, y });
				q0.push({ x + _x, y + _y });
				std::cout << "out\n";
			} else {
				vis[x][y] = 1;
				std::cout << "in\n";
			}
		}
	}
}
}  // namespace Qwarth

int main() {
	// clock_t START = clock();
	// freopen("gamble.in", "r", stdin);
	// freopen("gamble.out", "w", stdout);
	std::ios::sync_with_stdio(0), std::cin.tie(0);
	Qwarth::main();
	// std::cerr << 1.0 * (clock() - START) / CLOCKS_PER_SEC << " s\n";
	return 0;
}

自以为码风良好,希望各位读者看完我的题解都能通过这道题。

最后感谢洛谷管理员的审核。

原文