B - Valuable Resources

本文介绍了一种算法,用于解决计算机策略游戏中构建城市的问题。目标是最小化城市的面积,同时确保所有有价值的资源矿点位于城市内或边界上。通过读取输入坐标,算法计算并输出覆盖所有资源的最小城市面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Valuable Resources
Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems.
Let’s suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. The map contains mines with valuable resources, located at some points with integer coordinates. The sizes of mines are relatively small, i.e. they can be treated as points. The city should be built in such a way that all the mines are inside or on the border of the city square.
Building a city takes large amount of money depending on the size of the city, so you have to build the city with the minimum area. Given the positions of the mines find the minimum possible area of the city.

    Input
    The first line of the input contains number n — the number of mines on the map (2 ≤ n ≤ 1000). Each of the next n lines contains a pair of integers xi and yi — the coordinates of the corresponding mine ( - 109 ≤ xi, yi ≤ 109). All points are pairwise distinct.

    Output
    Print the minimum area of the city that can cover all the mines with valuable resources.

    Examples

Input

2
0 0
2 2

Output

4

Input

2
0 0
0 3

Output

9

题意是在输入的坐标中找出最长的x或y的坐标差的绝对值,但是要注意在加减的过程中会超出int范围,使用long long解决问题

#include"iostream"
#include"algorithm"
using namespace std;
int main()
{
	long long i,n,max1,x[1005],y[1005];
	i=0;
	cin>>n;
	while(i!=n)
	{
		cin>>x[i]>>y[i];
		i++;
	}
	sort(x,x+n);
	sort(y,y+n);
	max1=max(x[n-1]-x[0],y[n-1]-y[0]);
	cout<<max1*max1<<endl;
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值