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<stdio.h>
int main()
{
long long int n,a1=-1000000005,a2=-1000000005,b1=1000000005,b2=1000000005,x,y;
while(~scanf("%lld",&n))
{
for(int i=0;i<n;i++)
{
scanf("%lld %lld",&x,&y);
if(x>a1)
{
a1=x;
}
if(x<b1)
{
b1=x;
}
if(y>a2)
{
a2=y;
}
if(y<b2)
{
b2=y;
}
}
if(a2-b2>a1-b1)
{
printf("%lld\n",(a2-b2)*(a2-b2));
}
else
{
printf("%lld\n",(a1-b1)*(a1-b1));
}
}
return 0;
}