백준
백준 2628 종이자르기(Java)
Park DJ
2023. 1. 24. 01:03
https://www.acmicpc.net/problem/2628
2628번: 종이자르기
아래 <그림 1>과 같이 직사각형 모양의 종이가 있다. 이 종이는 가로방향과 세로 방향으로 1㎝마다 점선이 그어져 있다. 가로 점선은 위에서 아래로 1번부터 차례로 번호가 붙어 있고, 세로 점선
www.acmicpc.net
해석 및 팁
이 문제는 가로와 세로를 구분하여 ArrayList를 만들어준 후 처음에 0, 끝에는 가로와 세로의 길이를 더해서 정렬을 합니다. 그러고 나서 큰 것에서 작은 걸 빼고 그 차이를 비교하여 가장 차이가 큰 것을 max로 해서 가로와 세로의 max값을 곱해주면 됩니다.
Java 코드
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> width = new ArrayList<>();
ArrayList<Integer> heigh = new ArrayList<>();
width.add(0);
heigh.add(0);
int max_w = -1;
int max_h = -1;
int w = sc.nextInt();
int h = sc.nextInt();
int n = sc.nextInt();
for(int i = 0; i < n; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
if(a == 1) width.add(b);
else heigh.add(b);
}
width.add(w);
heigh.add(h);
Collections.sort(width);
Collections.sort(heigh);
for(int i = 0; i < width.size()-1; i++) {
if(max_w < (width.get(i+1)-width.get(i))) max_w = width.get(i+1) - width.get(i);
}
for(int i = 0; i < heigh.size()-1; i++) {
if(max_h < (heigh.get(i+1)-heigh.get(i))) max_h = heigh.get(i+1) - heigh.get(i);
}
System.out.println(max_w * max_h);
}
}