[swea-D3] 10580. 전봇대
SWEA 전봇대 문제를 JAVA를 이용해 완전탐색으로 해결하는 방법과 교차점 발생 규칙을 설명합니다.
🙋♂️ 들어가며
이 문제는 직접 그려보면 안다.
우선 이 선들을 연결해보자
1
2
3
4
5
6
1 10
2 2
3 1
4 3
5 7
6 4
그럼 다음과 같이 만들어진다.
📌i : 0
📌i : 1
📌i : 4
규칙이 보이지 않는가?
왼쪽 점에서는 -> (왼쪽 기준점 < 왼쪽 다른 점들)
오른쪽 점에서는 -> (우측 기준점 > 우측 다른 점들)
내 말이 맞는지 다른 testcase로 검증을 해보자
1
2
3 1
1 5
맞는 것 같다.
그래서 제출을 해보니 pass 더라
✅ 코드
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int tc = 1; tc < T+1; tc++) {
int N = Integer.parseInt(br.readLine());
// 1. 배열 채우기
int[] A = new int[10001];
int[] B = new int[10001];
for (int i = 0; i < N; i++) {
String[] ai_bi = br.readLine().split(" ");
int ai = Integer.parseInt(ai_bi[0]);
int bi = Integer.parseInt(ai_bi[1]);
A[i] = ai;
B[i] = bi;
}
// 2. 식
int cross_cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// 2-1. 본인이면 pass
if (i == j) continue;
// 2-2. 왼쪽점은 asc, 우측점은 desc
if (A[i] < A[j] && B[i] > B[j]) cross_cnt++;
}
}
System.out.println("#" + tc + " " + cross_cnt);
}
}
}
This post is licensed under CC BY 4.0 by the author.




