From 67e2175d566e5c2a0b29219e3a8afcede399d2a6 Mon Sep 17 00:00:00 2001 From: ManduTheCat Date: Sat, 22 Oct 2022 00:25:32 +0900 Subject: [PATCH] =?UTF-8?q?Week10=20=ED=86=B5=ED=95=99=EB=B2=84=EC=8A=A4?= =?UTF-8?q?=202513=20=EA=B7=B8=EB=A6=AC=EB=94=94=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/week10/tonghagbus2513/Main.java | 100 ++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/week10/tonghagbus2513/Main.java b/src/week10/tonghagbus2513/Main.java index 5de49ef..a022c07 100644 --- a/src/week10/tonghagbus2513/Main.java +++ b/src/week10/tonghagbus2513/Main.java @@ -1,5 +1,105 @@ package week10.tonghagbus2513; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.PriorityQueue; +import java.util.StringTokenizer; + + +class Bus { + int maxStudent; + int currStudent; + + public Bus(int maxStudent, int currStudent) { + this.maxStudent = maxStudent; + this.currStudent = currStudent; + } + +} + +class AptPoint implements Comparable { + int studentNum; + int dist; + int point; + + public AptPoint(int studentNum, int dist, int point) { + this.studentNum = studentNum; + this.dist = dist; + this.point = point; + } + + @Override + public int compareTo(AptPoint o) { + return o.dist - this.dist; + } +} + public class Main { + static int N, K, S; + static PriorityQueue left; + static PriorityQueue right; + static Bus bus; + + public static void main(String[] args) throws IOException { + //System.setIn(new FileInputStream("resources/study/week10/BJ2513/input2.txt")); + 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()); // 학교위치 + left = new PriorityQueue<>(); + right = new PriorityQueue<>(); + bus = new Bus(K, 0); + for (int row = 0; row < N; row++) { + st = new StringTokenizer(br.readLine()); + int point = Integer.parseInt(st.nextToken()); + int student = Integer.parseInt(st.nextToken()); + int dist = Math.abs(S - point); + if (point < S) { + // 왼쪽에 있다 + left.offer(new AptPoint(student, dist, point)); + } else { + // 오른쪽에 있다 + right.offer(new AptPoint(student, dist, point)); + } + } + + int lDIst = countDist(left); + int RDIst = countDist(right); + System.out.println((lDIst + RDIst)); + + + } + + private static int countDist(PriorityQueue pq) { + int totalDist = 0; + while (!pq.isEmpty()) { + AptPoint curStop = pq.poll(); + int repeatCount = curStop.studentNum / bus.maxStudent; // 반복해야하는 횟수 + if (curStop.studentNum % bus.maxStudent > 0) repeatCount++; // 나머지가 있다면 한번더 올수 있는거다 + // 태울수 있는 버스인원 = 왕복하면서 태운 학생수 - 현재 정류장인원 + // 만약 10 인데 4인버스라면 3번간다 (12) - 현재 정류장인웡 = 버스내의 승객은 2명더태울수 있다. + int remainStudent = bus.maxStudent * repeatCount - curStop.studentNum; + while (!pq.isEmpty()) { + // 다음 정류장을 돌면서 더태울수 있는지 탐색한다. + AptPoint next = pq.poll(); + // 태울수 있는 인원수가 더많으면 + if (next.studentNum <= remainStudent) { + //버스에 태운다 + remainStudent -= next.studentNum; + } else { + // 태울수 있는 인원이 다음 역보다 적으면 + // 남은 승객수만큼만 태우고 다시온다 + next.studentNum -= remainStudent; + pq.add(next); + break; + } + } + totalDist += (2 * repeatCount * curStop.dist); + } + return totalDist; + } + }