Square Pasture
Solución en video
Por Maggie Liu
Video de YouTube (gXNTflZeutk)
Código de la solución en video
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
freopen("square.in", "r", stdin);
freopen("square.out", "w", stdout);
int x1, y1, x2, y2;
int x3, y3, x4, y4;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
int left, right, top, bottom;
// find the sides of the smallest rectangle covering both pastures
left = min(x1, x3);
right = max(x2, x4);
bottom = min(y1, y3);
top = max(y2, y4);
/*
* the smallest square will need a side length
* that is the maximum of the side lengths of the rectangle
*/
int side = max(right - left, top - bottom);
cout << side * side << endl;
}import java.io.*;
import java.util.*;
public class SquarePasture {
public static void main(String[] args) throws IOException {
Kattio io = new Kattio("square");
int x1 = io.nextInt(), y1 = io.nextInt();
int x2 = io.nextInt(), y2 = io.nextInt();
int x3 = io.nextInt(), y3 = io.nextInt();
int x4 = io.nextInt(), y4 = io.nextInt();
// find the sides of the smallest rectangle covering both pastures
int left = Math.min(x1, x3);
int right = Math.max(x2, x4);
int bottom = Math.min(y1, y3);
int top = Math.max(y2, y4);
/*
* the smallest square will need a side length
* that is the maximum of the side lengths of the rectangle
*/
int side = Math.max(right - left, top - bottom);
io.println(side * side);
io.close();
}
// CodeSnip{Kattio}
}import sys
sys.stdin = open("square.in", "r")
sys.stdout = open("square.out", "w")
x1, y1, x2, y2 = map(int, input().split())
x3, y3, x4, y4 = map(int, input().split())
# find the sides of the smallest rectangle covering both pastures
left = min(x1, x3)
right = max(x2, x4)
bottom = min(y1, y3)
top = max(y2, y4)
# the smallest square will need a side length
# that is the maximum of the side lengths of the rectangle
side = max(right - left, top - bottom)
print(side * side)Explicación
Primero podemos hallar el rectángulo más chico que cubre ambos pastizales. Este rectángulo tiene que cubrir el menor de los lados izquierdos de ambos pastizales y el mayor de los lados derechos de ambos pastizales. También tiene que cubrir el menor de los lados inferiores y el mayor de los lados superiores de ambos pastizales. El cuadrado más chico tiene un lado igual al lado más largo del rectángulo.
Implementación
Complejidad temporal:
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
freopen("square.in", "r", stdin);
freopen("square.out", "w", stdout);
int x1, y1, x2, y2;
int x3, y3, x4, y4;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
int left, right, top, bottom;
// find the sides of the smallest rectangle covering both pastures
left = min(x1, x3);
right = max(x2, x4);
bottom = min(y1, y3);
top = max(y2, y4);
/*
* the smallest square will need a side length
* that is the maximum of the side lengths of the rectangle
*/
int side = max(right - left, top - bottom);
cout << side * side << endl;
return 0;
}import java.io.*;
import java.util.*;
public class Square {
public static void main(String[] args) throws IOException {
Kattio io = new Kattio("square");
int x1 = io.nextInt(), y1 = io.nextInt();
int x2 = io.nextInt(), y2 = io.nextInt();
int x3 = io.nextInt(), y3 = io.nextInt();
int x4 = io.nextInt(), y4 = io.nextInt();
// find the sides of the smallest rectangle covering both pastures
int left = Math.min(x1, x3);
int right = Math.max(x2, x4);
int bottom = Math.min(y1, y3);
int top = Math.max(y2, y4);
/*
* the smallest square will need a side length
* that is the maximum of the side lengths of the rectangle
*/
int side = Math.max(right - left, top - bottom);
io.println(side * side);
io.close();
}
// CodeSnip{Kattio}
}import sys
sys.stdin = open("square.in", "r")
sys.stdout = open("square.out", "w")
x1, y1, x2, y2 = map(int, input().split())
x3, y3, x4, y4 = map(int, input().split())
# find the sides of the smallest rectangle covering both pastures
left = min(x1, x3)
right = max(x2, x4)
bottom = min(y1, y3)
top = max(y2, y4)
# the smallest square will need a side length
# that is the maximum of the side lengths of the rectangle
side = max(right - left, top - bottom)
print(side * side)