列表

详情


NC24289. [USACO 2016 Dec B]Square Pasture

描述

Farmer John has decided to update his farm to simplify its geometry. Previously, his cows grazed in two rectangular fenced-in pastures. Farmer John would like to replace these with a single square fenced-in pasture of minimum size that still covers all the regions of his farm that were previously enclosed by the former two fences.
Please help Farmer John figure out the minimum area he needs to make his new square pasture so that if he places it appropriately, it can still cover all the area formerly covered by the two older rectangular pastures. The square pasture should have its sides parallel to the x and y axes.

输入描述

The first line in the input file specifies one of the original rectangular pastures with four space-separated integers x1 y1 x2 y2, each in the range 0…10. The lower-left corner of the pasture is at the point (x1,y1), and the upper-right corner is at the point (x2,y2), where x2>x1 and y2>y1.
The second line of input has the same 4-integer format as the first line, and specifies the second original rectangular pasture. This pasture will not overlap or touch the first pasture.

输出描述

The output should consist of one line containing the minimum area required of a square pasture that would cover all the regions originally enclosed by the two rectangular pastures.

示例1

输入:

6 6 8 8
1 8 4 9

输出:

49

说明:

In the example above, the first original rectangle has corners (6,6)(6,6) and (8,8)(8,8). The second has corners at (1,8)(1,8) and (4,9)(4,9). By drawing a square fence of side length 7 with corners (1,6)(1,6) and (8,13)(8,13), the original areas can still be enclosed; moreover, this is the best possible, since it is impossible to enclose the original areas with a square of side length only 6. Note that there are several different possible valid placements for the square of side length 7, as it could have been shifted vertically a bit.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 2ms, 内存消耗: 480K, 提交时间: 2020-08-07 09:35:11

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,y,x1,y1,xx,xx1,yy,yy1;
	cin>>x>>y>>x1>>y1;
	cin>>xx>>yy>>xx1>>yy1;
	x=min(x,xx),x1=max(x1,xx1);
	y=min(y,yy),y1=max(y1,yy1);
	int max1=max(abs(x-x1),abs(y-y1));
	printf("%d\n",max1*max1);
}

C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 400K, 提交时间: 2020-08-05 20:39:18

#include<iostream>
using namespace std;
int main() {
    int a,b,c,d,e,f,g,h;
    cin>>a>>b>>c>>d>>e>>f>>g>>h;
    int x1 = min(a,e), y1 = min(b,f), x2 = max(c,g), y2 = max(d,h);
    int res = max(x2-x1,y2-y1);
    cout<<res*res;
    return 0;
}

Python3(3.5.2) 解法, 执行用时: 20ms, 内存消耗: 3364K, 提交时间: 2020-08-07 10:38:53

x1, y1, x2, y2 = list(map(int, input().split()))
a1, b1, a2, b2 = list(map(int, input().split()))
x0 = min(x1, a1)
y0 = min(y1, b1)
x = max(x2, a2)
y = max(y2, b2)
print(max(abs(y - y0),abs(x - x0)) ** 2)

上一题