[๋ฐฑ์ค, Java] 5186๋ฒ : ํํฐ๋ฅผ ์ด์ด๋ผ!!!
๐ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/5186
๐ฎ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
์ต๋ํ ๋ง์ ์ฐจ๋ฅผ ์ด์ฉํด ์ต๋ํ ๋ง์ ์น๊ตฌ๋ค์ ์ง์ ๋ณด๋ด์ค์ผ ์ต์ํ์ผ๋ก ์ง์์ ์ฌ์ธ ์ ์๋ค.
์ต๋ํ ๋ง์ ์น๊ตฌ๋ค์ ๋ณด๋ด์ผํ๊ธฐ ๋๋ฌธ์ “์ฐจ์ ์ข์์๊ฐ ๋ง์ ์์ผ๋ก ๋ด๋ฆผ์ฐจ์" ์ ๋ ฌ์ ํ๋ค.
๋ํ, ์ด์ ํ ์ ์๋ ์น๊ตฌ๋ค์ ๋จผ์ ๋ฐฐ์นํด์ฃผ๊ธฐ ์ํด์ ์ ์ ๋ง์์ง ์์ (S)๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ค.
์๋์ฐจ์ ๊ฒฝ์ฐ ์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํด ํ๋์ฉ ๋นผ๋ฉด์ ํ์ธ ์ ์๋ ์ด์ ์๋ฅผ ๋ฐฐ์นํ๋ค. ๋ฐฐ์น๋ ์ด์ ์๋ ์ฐจ์ ํ๋ค๋ ์ฒดํฌ(ride)๋ฅผ ํด์ฃผ๊ณ ์ด์ ์ ์ธ์์๋ฅผ 1 ์ฆ๊ฐ์ํจ๋ค.
์ด์ ์๊ฐ ๋ฐฐ์น๋๋ ๊ฒฝ์ฐ “ํ์ธ ์ ์๋ ์ข์ - 1 (์ด์ ์)”๋ฅผ ์ง์ญ๋ณ ํ์ธ ์ ์๋ ์ธ์์ ๋ฐฐ์ด(avilable)์ ์ ์ฅํ๋ค.
์ต๋ํ ๋ง์ ์ด์ ์๋ฅผ ๋ฐฐ์นํ๋ค๋ฉด, ์ด์ ์น๊ตฌ๋ค ์ธ์๋งํผ ๋ฐ๋ณตํ๋ฉด์ ์์ง ์ฐจ์ ํ์ง ์์ ์ ๋ค์ ๋ชฉ์ ์ง์ ๋์ผํ ์ง์ญ์ ์ฐจ์ ์ข์์ด ๋จ์์๋ค๋ฉด ๋ฃ์ด์ฃผ๊ณ , ์๋ ๊ฒฝ์ฐ ์นด์ดํ ํด์ค๋ค.
์์ง ์ฐจ์ ํ์ง ์์ ๊ฒฝ์ฐ์๋ง ์ ๋ค์ ํ์ฐ๋ ์กฐ๊ฑด์ผ๋ก ํ๊ธฐ ๋๋ฌธ์ ์ด์ ์๋ค๋ ํจ๊ป ์นด์ดํ ์ด๋๋ค.
์ด ๋, ์ด์ ์๋ฅผ ๋ฐฐ์นํ๋ฉด์ ์นด์ดํ ํ ์ด์ ์ ์ธ์์๋ฅผ ๋นผ์ค๋ค.
- ์น๊ตฌ๋ค ๋ฆฌ์คํธ๋ฅผ ์ ๋ ฅ๋ฐ์ผ๋ฉด์ ์ ์ ์์ทจํ ์น๊ตฌ๋ค์ ์นด์ดํ ํ๋ค.
- ์๋์ฐจ ๋ฆฌ์คํธ์ ์น๊ตฌ๋ค ๋ฆฌ์คํธ๋ ๊ฐ๊ฐ “์๋์ฐจ์ ์ข์ ๋ด๋ฆผ์ฐจ์”, “์ ์ ์์ทจํ ์น๊ตฌ๋ค"๋ก ์ ๋ ฌํ๋ค.
- ์๋์ฐจ๋ฅผ ํ์์ ํ๋์ฉ ๋นผ๋ฉด์ ์ด์ ์๋ฅผ ํ์ธ ์ ์๋์ง ํ๋จํ๊ณ , ํ์ธ ์ ์๋ค๋ฉด ์ด์ ์๊ฐ ํ๋ค๋ ํ์๋ฅผ ํ๊ณ ์ด์ ์๋ฅผ ์ ์ธํ ๋จ์ ์ข์์ ๋ฐฐ์ด์ ์ฆ๊ฐ์ํจ๋ค. ์ด ๋, ์ด์ ์์ ์๋ ์นด์ดํ ํ๋ค.
- ์น๊ตฌ๋ค ์ธ์๋งํผ ๋ฐ๋ณตํ๋ฉด์ ์ฐจ์ ํ์ง ์์ ์น๊ตฌ๋ค์ ๋ชฉ์ ์ง์ ๋์ผํ ์ฐจ์ ์ข์์ด ๋จ์์๋ค๋ฉด ํ์ด๋ค.
- ์ด๋ฏธ ์ฐจ์ ํ๊ฑฐ๋, ํ์ ์๋ ๊ฒฝ์ฐ ์นด์ดํ ์ ํ๋ค.
- ์ด๋ฏธ ์ฐจ์ ํ ๊ฒฝ์ฐ๋ ํจ๊ป ์นด์ดํ ๋๊ธฐ ๋๋ฌธ์ ์ด์ ์๋ค์ ์ธ์์ ๋งํผ ๋นผ์ค๋ค. (์ด์ ์๋ ์ฐจ์ ํ์ง๋ง ์นด์ดํ ์ ํฌํจ๋๊ธฐ ๋๋ฌธ์ด๋ค.)
๐ฉ Java ์ฝ๋
package com.second.algorithm.baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main_5186_ํํฐ๋ฅผ_์ด์ด๋ผ {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int K = Integer.parseInt(br.readLine()); // ํ
์คํธ์ผ์ด์ค
for (int cases = 1; cases <= K; cases++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken()); // ์ฌ๋์
int C = Integer.parseInt(st.nextToken()); // ์๋์ฐจ ์
int L = Integer.parseInt(st.nextToken()); // ์ง์ญ ์
List<Friend> friends = new ArrayList<>();
int driver = 0;
for (int count = 0; count < N; count++) {
st = new StringTokenizer(br.readLine());
int location = Integer.parseInt(st.nextToken());
String drunk = st.nextToken();
// ์ด์ ํ ์ ์๋ ์ต๋ ์ธ์์ ๊ตฌํด๋๋๋ค.
if(drunk.equals("S")) driver++;
friends.add(new Friend(location, drunk));
}
List<Car> cars = new ArrayList<>();
for (int count = 0; count < C; count++) {
st = new StringTokenizer(br.readLine());
int location = Integer.parseInt(st.nextToken());
int available = Integer.parseInt(st.nextToken());
cars.add(new Car(location, available));
}
int sleepPeople = sleepPeople(N, C, L, friends, cars, driver);
System.out.println("Data Set "+cases +":");
System.out.println(sleepPeople);
}
br.close();
}
private static int sleepPeople(int N, int C, int L, List<Friend> friends, List<Car> cars, int driver) {
// ์ฐจ๊ฐ ์๋ ๊ฒฝ์ฐ
if(C == 0) return N;
PriorityQueue<Car> carQueue = new PriorityQueue<>(cars);
Collections.sort(friends);
int[] available = new int[L+1]; // ์ฐจ์ ํ์ธ ์ ์๋ ์ธ์ ๋ฐฐ์ด
boolean[] ride = new boolean[N]; // ์น๊ตฌ๋ค์ด ํ๋์ง ์ํ๋์ง ํ๋จํ๋ ๋ฐฐ์ด
int rideDriver = 0;
while(!carQueue.isEmpty()) {
Car car = carQueue.poll();
// ์ด์ ์๋ง ๋จผ์ ํ์ธ์ ์๋ ๋งํผ ๋ฃ์ด์ฃผ๊ธฐ
for (int number = 0; number < driver; number++) {
if(car.getLocation() == friends.get(number).getLocation() && !ride[number]) {
available[car.getLocation()] += car.getAvailable() - 1; // ์์น๋ณ ์ด์ ์ ์ ์ธํ๊ณ ์๋์ฐจ์ ํ ์ ์๋ ์ธ์์ ์ถ๊ฐ
ride[number] = true;
rideDriver++; // ์ด์ ํ๋ ์น๊ตฌ๋ค ์นด์ดํ
break;
}
}
}
int count = 0;
for (int number = 0; number < friends.size(); number++) {
Friend friend = friends.get(number);
if(available[friend.getLocation()] > 0 && !ride[number] ) available[friend.getLocation()]--; // ์๋ฆฌ๊ฐ ๋จ์๊ณ , ์์ง ์ฐจ์ ์ํ ์น๊ตฌ๋ฉด ํ์ฐ๊ธฐ
else count++;
}
// ์ด์ ์ ์น๊ตฌ๋ค์ ์ด๋ฏธ ride ๋ฐฐ์ด์ด true์ด๋ฏ๋ก ์์ ์น๊ตฌ๋ค์ ํ์ธ ๋ ์ด์ ์๋ ๊ฐ์ด ์นด์ดํ
๋๋ค.
// ๋ฐ๋ผ์, ์ด์ ์ ์น๊ตฌ๋ค ๋งํผ ๋นผ์ค๋ค.
return count - rideDriver;
}
static class Friend implements Comparable<Friend> {
private int location;
private String drunk;
public Friend(int location, String drunk) {
this.location = location;
this.drunk = drunk;
}
public int getLocation() {
return location;
}
public String getDrunk() {
return drunk;
}
@Override
public int compareTo(Friend o) {
if(o.drunk.equals(this.drunk))
return Integer.compare(this.location, o.location);
return o.drunk.compareTo(this.drunk);
}
}
static class Car implements Comparable<Car> {
private int location;
private int available;
public Car(int location, int available) {
this.location = location;
this.available = available;
}
public int getLocation() {
return location;
}
public int getAvailable() {
return available;
}
@Override
public int compareTo(Car o) {
if(this.location == o.location)
return Integer.compare(o.available, this.available);
return Integer.compare(this.location, o.location);
}
}
}