跳到正文

题解

【LABOI 2026】过客 / guest 题解

__biu_biu_biu__约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年2月21日
收录
2026年10月3日
更新
2026年11月11日
篇幅
约 2 分钟

版本

  1. v1当前

出题人题解。

为了方便,我们先特判小 Z 一开始在小 C 的视野范围内的情况。

先计算出小 Z 走到小 C 视野范围内的答案,为 max⁡(∣x−s∣+∣y−t∣−k,0)\max(|x-s|+|y-t|-k,0),注意你此时走的最后一步是有贡献的,因为你的落点在小 C 的视野范围内。

此时我们可以得到小 Z 剩下可以走的步数 l′l',然后在范围内随意乱走,只要不出小 C 的视野范围即可。

注意特判 k=0k=0 的情况,这样的话由于无法不走,所以必须走出视野范围减少贡献,假设小 Z 在小 C 的视野范围内,走了 rr 步,则实际的贡献只有 ⌊r2⌋\lfloor \frac{r}{2} \rfloor 步。

代码
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int T;
	cin >> T;
	while (T--) {
		ll x, y, s, t, k, l;
		cin >> x >> y >> s >> t >> k >> l;
		ll dis = abs(x - s) + abs(y - t);
		if(dis <= k) {//特判一开始小 Z 就在小 C 视野范围内的情况
			if(k == 0) cout << l / 2 << '\n';
			else cout << l << '\n';
			continue ;
		}
		l -= (dis - k);//剩余步数
		if(l < 0) {
			cout << 0 << '\n';
			continue ;
		}
		if(k == 0) {
			cout << 1 + l / 2 << '\n';
		}
		else cout << 1 + l << '\n';//注意添加一开始走到视野范围内时的 1 贡献
	}
	return 0;
}
原文