跳到正文

题解

题解:P17710 Red Face 操作点(point)

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

文章信息

发布
2026年10月16日
收录
2026年10月16日
更新
2026年11月7日
篇幅
约 2 分钟
题目
P17710

版本

  1. v1当前

还不如多搬点 htbc 和 tctm 的题目

思路

问题可以视为固定 x,yx,y 较大的点,移动另一个点。

设 X,YX,Y 分别为两点横纵左边的差,那么求的是 S=X2+Y2S=X^2+Y^2 的最小值。

当 X−1X-1 后,SS 相较于原来少了 2X−12X-1。那么可以考虑贪心,优先减 X,YX,Y 中较大的。

移动次数 kk 最后还有余咋办?这意味着两个点已经重合,考虑让他们交替按同一方向移动,这样能保证最小。多余的 kk 为偶数时,答案为 00;否则为 11。证明显然。

代码

code
C++
// code by Umbrella.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,l,r) for(int i=(l);i<=(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define pii pair<int,int>
#define fir first
#define sec second
#define pb push_back
#define mkp make_pair
namespace Umbrella{
	int x1,y1,x2,y2,k;
	void solve(){
		cin>>x1>>y1>>x2>>y2>>k;
		int x=abs(x1-x2);
		int y=abs(y1-y2);
		while(k&&(x||y)){
			if(x>=y) x--;
			else y--;
			k--;
		}
        if(k&1) cout<<"1\n";
		else cout<<x*x+y*y<<'\n';
	}
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int _T=1;
	cin>>_T;
	while(_T--){
		Umbrella::solve();
	}
	return 0;
}
原文