• 문제 링크
25972번: 도미노 무너트리기
미야노는 $N$개의 도미노를 가지고 놀고 있다. 각각의 도미노는 1차원 좌표계의 $x$좌표 위에 위치하고 있고 길이를 가진다. $i$번째 도미노의 $x$좌표를 $a_i$, 길이를 $l_i$라 하자. 도미노는 오른쪽
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read();
Pair[] arr = new Pair[n];
while (n-- > 0) arr[n] = new Pair(read(), read());
Arrays.sort(arr);
int cnt = 1;
for (int i = 0; i < arr.length - 1; i++) if (arr[i].a + arr[i].l < arr[i + 1].a) cnt++;
bw.write(String.valueOf(cnt));
bw.flush();
}
private static class Pair implements Comparable<Pair> {
int a, l;
Pair(int a, int l) {
this.a = a;
this.l = l;
}
@Override
public int compareTo(Pair o) {
return a - o.a;
}
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 8582 Park - Dynamic Programming / Java (0) | 2024.02.17 |
---|---|
[백준] 6092 Strange Towers of Hanoi - Dynamic Programming / Java (0) | 2024.02.16 |
[백준] 16237 이삿짐센터 - Greedy / Java (0) | 2024.02.14 |
[백준] 16200 해커톤 - Greedy / Java (0) | 2024.02.13 |
[백준] 14720 우유 축제 - Greedy / Java (0) | 2024.02.12 |
댓글