[BaekJoon] #9063 - 대지 [Java][C++][Python]
[BaekJoon] #9063 - 대지 [Java][C++][Python]
1. 아이디어
모든 구슬 위치를 포함하면서 변이 축에 평행한 가장 작은 직사각형의 넓이를 구하는 문제다. 직사각형이 모든 점을 담으려면 가로 범위가 가장 작은 $x$좌표부터 가장 큰 $x$좌표까지를 포함해야 하고 세로도 마찬가지이므로, 그 범위에 딱 맞춘 직사각형이 가장 작다. 따라서 $x$좌표와 $y$좌표의 최솟값과 최댓값을 각각 구한 뒤, 최댓값에서 최솟값을 뺀 가로와 세로 길이를 곱하면 된다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int minX = 10000;
int minY = 10000;
int maxX = -10000;
int maxY = -10000;
int n = Integer.parseInt(br.readLine());
while (n-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
System.out.println((maxX - minX) * (maxY - minY));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int mnX = 10000;
int mnY = 10000;
int mxX = -10000;
int mxY = -10000;
int n;
cin >> n;
while (n--) {
int x, y;
cin >> x >> y;
mnX = min(mnX, x);
mnY = min(mnY, y);
mxX = max(mxX, x);
mxY = max(mxY, y);
}
cout << (mxX - mnX) * (mxY - mnY);
}
1
2
3
4
5
6
7
8
9
10
11
12
import sys
input = sys.stdin.readline
xs, ys = [], []
n = int(input())
for _ in range(n):
x, y = map(int, input().split())
xs.append(x)
ys.append(y)
print((max(xs) - min(xs)) * (max(ys) - min(ys)))
This post is licensed under CC BY 4.0 by the author.