-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_2513.java
More file actions
68 lines (55 loc) · 2.29 KB
/
BOJ_2513.java
File metadata and controls
68 lines (55 loc) · 2.29 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class BOJ_2513 {
public static int N, K, S, minDist = 0;
public static void main(String[] args) throws IOException {
//입력
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken()); //아파트 단지 수
K = Integer.parseInt(st.nextToken()); //통학버스 정원
S = Integer.parseInt(st.nextToken()); //학교 위치
List<int[]> leftStudents = new ArrayList<>(); //위치, 학생 수
List<int[]> rightStudents = new ArrayList<>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int location = Integer.parseInt(st.nextToken());
int number = Integer.parseInt(st.nextToken());
if (location >= S) rightStudents.add(new int[]{location, number});
else leftStudents.add(new int[]{location, number});
}
//구현
Collections.sort(leftStudents, (a, b) -> a[0] - b[0]); //오름차순
Collections.sort(rightStudents, (a, b) -> b[0] - a[0]); //내림차순
calcDist(leftStudents);
calcDist(rightStudents);
System.out.println(minDist);
}
private static void calcDist(List<int[]> students) {
int bs = 0; // 지금 버스에 타있는 학생 수
int far = 0; // 지금 가장 먼 거리
for(int[] s : students) {
int loc = s[0], num = s[1];
while(num > 0){
if(bs == 0) far = loc; // 방금 출발
// 최대는 가능한 수, 안되면 현재 내 수
int take = Math.min(num, K - bs);
num -= take;
bs += take;
// 정원 다 참 -> 다 내림
if(bs == K){
minDist += Math.abs(S - far) * 2;
far = 0; bs = 0;
}
}
//System.out.println(minDist);
}
// 버스에 타있는 학생
if(bs > 0) minDist += Math.abs(S - far) * 2;
}
}