跳到正文

题解

题解:CF1979E Manhattan Triangle

喵仔牛奶约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年4月6日
收录
2025年12月12日
更新
2026年9月14日
篇幅
约 4 分钟
获赞
1
题目
CF1979E

版本

  1. v1当前

Solution

先转切比雪夫。

讨论取到 max⁡\max 的是 xx 坐标还是 yy 坐标,可以发现不可能三个都取同一个坐标。因此,一定是两个取 xx 一个取 yy,或者两个取 yy 一个取 xx。

考虑两个取 xx 一个取 yy,另一种情况交换 x,yx,y 再跑一遍即可。

设 (A,B),(B,C)(A,B),(B,C) 两对取了 xx 坐标,那么不可能 xB=xA−d,xC=xA+dx_B=x_A-d,x_C=x_A+d, 不然 B,CB,C 距离有 2d2d。因此有 xB=xCx_B=x_C。

钦定 yB<yCy_B<y_C,枚举 BB,此时可以知道 C=(xB,yB+d)C=(x_B,y_B+d)。那么合法的 AA 条件就是 ∣xA−xB∣=d∧yA∈[xB,xC]\lvert x_A-x_B\rvert =d\land y_A\in[x_B,x_C]。用 set 维护 sis_i 表示 xj=ix_j=i 的点 jj 的 yjy_j 的集合,枚举 xA=xB−dx_A=x_B-d 还是 xA=xB+dx_A=x_B+d,在 set 里二分看看有没有即可。

代码比较短。

Code

C++
#include <bits/stdc++.h>
#define REP(i, l, r) for (int i = (l); i <= (r); ++ i)
#define DEP(i, r, l) for (int i = (r); i >= (l); -- i)
#define fi first
#define se second
#define pb emplace_back
#define mems(x, v) memset((x), (v), sizeof(x))
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
using namespace std;
namespace Milkcat {
	typedef long long LL;
	typedef pair<LL, LL> pii;
	const int N = 1e6 + 5;
	int n, d, x, y, A, B, C, a[N], b[N];
	map<int, set<int>> mp; map<pii, int> id;
	int main() {
		cin >> n >> d, A = B = C = 0;
		REP(i, 1, n) {
			cin >> x >> y;
			a[i] = x + y, b[i] = x - y;
		}
		REP(t, 0, 3) {
			int dx = (t & 1 ? -d : d);
			REP(i, 1, n)
				mp[a[i]].insert(b[i]), id[{a[i], b[i]}] = i;
			for (auto &[x, s] : mp) {
				if (!mp.count(x + dx)) continue;
				auto &t = mp[x + dx];
				for (int y : s) {
					if (!s.count(y + d)) continue;
					auto it = t.lower_bound(y);
					if (it == t.end() || *it > y + d) continue;
					A = id[{x, y}], B = id[{x, y + d}], C = id[{x + dx, *it}]; break;
				}
			}
			mp.clear(), id.clear();
			if (A > 0) break;
			if (t == 1) REP(i, 1, n) swap(a[i], b[i]);
		}
		cout << A << ' ' << B << ' ' << C << '\n'; 
		return 0;
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	int T = 1; cin >> T;
	while (T --) Milkcat::main();
	return 0;
}
原文