Skip to content

Commit 78b8b26

Browse files
authored
[20251017] BOJ / G2 / 구간 자르기 / 김수연
1 parent 5b3cd05 commit 78b8b26

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
```java
2+
package boj;
3+
4+
import java.io.*;
5+
import java.util.*;
6+
7+
public class boj2283 {
8+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
static StringTokenizer st;
10+
static void nextLine() throws Exception { st = new StringTokenizer(br.readLine()); }
11+
static int nextInt() { return Integer.parseInt(st.nextToken()); }
12+
13+
public static void main(String[] args) throws Exception {
14+
nextLine();
15+
int N = nextInt();
16+
int K = nextInt();
17+
int start= 1000001;
18+
int end = 0;
19+
int[] num = new int[1000001];
20+
// 누적합 생성
21+
for (int i = 0; i < N; i++) {
22+
nextLine();
23+
int a = nextInt();
24+
int b = nextInt();
25+
num[a]++;
26+
num[b]--;
27+
start = Math.min(start, a);
28+
end = Math.max(end, b);
29+
}
30+
for (int i = start+1; i <= end; i++) {
31+
num[i] += num[i-1];
32+
}
33+
34+
// 투 포인터
35+
int sum = 0;
36+
int s = 0;
37+
int e = start;
38+
while (e <= end) {
39+
if (sum < K) {
40+
sum += num[e];
41+
e++;
42+
} else if (sum == K) {
43+
System.out.println(s+" "+e);
44+
return;
45+
} else {
46+
sum -= num[s];
47+
s++;
48+
}
49+
}
50+
System.out.println(0+" "+0);
51+
}
52+
}
53+
```

0 commit comments

Comments
 (0)