[Java] ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ | ๋ฐฑ์ค 2457 ๊ณต์ฃผ๋์ ์ ์
https://www.acmicpc.net/problem/2457
์ ๊ทผ ๋ฐฉ๋ฒ
3์ 1์ผ๋ถํฐ 11์ 30์ผ๊น์ง ๋งค์ผ ๊ฝ์ด ํ ๊ฐ์ง ์ด์ ํผ์ด์์ด์ผ ํ๋ฏ๋ก
๊ฝ ํผ๋ ๋ ์ง ์ค ํ๋๋ 3์ 1์ผ ์ดํ, ์ง๋ ๋ ์ง ์ค ํ๋๋ ๋ฌด์กฐ๊ฑด 12์ ์ด์์ด์ด์ผ ํ๋ค.
3์ 1์ผ ์ดํ ๋ ์ง์ ํผ๋ ๊ฝ๋ค ์ค ์ด๋ค ๊ฝ์ ๊ณจ๋ผ์ผ ๋ ์ง ์๊ฐํด๋ณด๋ฉด, ๊ทธ ์ค์์๋ ๋ฌด์กฐ๊ฑด ๊ฐ์ฅ ๋ฆ๊ฒ ์ง๋ ๊ฝ์ ๊ณ ๋ฅด๋ ๊ฒ์ด ๊ฝ๋ค์ ์๋ฅผ ๊ฐ๋ฅํ ์ ๊ฒ ํ๋ ์กฐ๊ฑด์ ๋ง์กฑ์ํค๊ธฐ ์ ๋ฆฌํ๋ค.
๊ทธ๋ ๊ฒ ์ฒ์ ์ ํํ ๊ฝ์ ์ ์ ํ๋ค๋ฉด, ๊ทธ ์ ํ๋ ๊ฝ์ ์ง๋ ๋ ์ง๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ค์ ์ ํ๋ ๊ฝ์ ์ ์ ํ๋ฉด ๋ ๊ฒ์ด๋ค.
๊ตฌ๊ฐ์ ๊ฒน์น ์ ์๊ฒ ๋ฝ๋, ์ง๋ ๋ ์ง๊ฐ ๊ธด ๊ฝ์ ์ ํ.
๊ณผ์ ์ ๋ฐ๋ณตํ๋ฉฐ ์นด์ดํธ๋ฅผ ํ๊ณ , ์ง๋ ๋ ์ง๊ฐ 12์ ์ด์์ธ ๊ฝ์ ์ ํํ์ ๋ ๋ฐ๋ณต์ ์ข ๋ฃํ๊ณ ์นด์ดํธ๋ฅผ ์ถ๋ ฅํ๋ค.
(๊ทธ ์ด์ ์ ์ ํ ๊ท์น์ ์ ํฉํ ๊ฝ์ด ์กด์ฌํ์ง ์๋๋ค๋ฉด ์ฆ์ 0์ ์ถ๋ ฅํ๊ณ ํ๋ก์ธ์ค๋ฅผ ์ข ๋ฃํ๋ค.)
์ด ๊ณผ์ ์ ๊ฐ๋ฅํ๊ฒ ํ๋ ค๋ฉด ๊ฝ์ ์์์ผ ๊ธฐ์ค์ผ๋ก ์ฐ์ ๋์ดํ ์ ์์ด์ผ ํ๋ฏ๋ก Flower ํด๋์ค ๋ด์ ์์์ผ ๊ธฐ์ค์ผ๋ก ๊ฝ์ ์ ๋ ฌํ ์ ์๋๋ก Comparable ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋ค.
static class Flower implements Comparable<Flower>{
int[] start = new int[2];
int[] end = new int[2];
public Flower(int start_m, int start_d, int end_m, int end_d){
this.start[0] = start_m;
this.start[1] = start_d;
this.end[0] = end_m;
this.end[1] = end_d;
}
//์์์ผ ๊ธฐ์ค
@Override
public int compareTo(Flower other){
if(this.start[0] != other.start[0]){
return Integer.compare(this.start[0], other.start[0]);
}else if(this.start[1]!=other.start[1]){
return Integer.compare(this.start[1], other.start[1]);
}else if(this.end[0]!=other.end[0]){
return Integer.compare(other.end[0], this.end[0]);
}else{
return Integer.compare(other.end[1], this.end[1]);
}
}
}
์๋์ ๊ฐ์ด main ๋ฉ์๋์์ ๊ฝ์ ์ ๋ ฅ ๋ฐ๊ณ ๋ฆฌ์คํธ์ ๋ฃ์ด ์ค ๋ค ์๊ฐ์ผ ๊ธฐ์ค์ผ๋ก ๋์ด์ํจ๋ค.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
List<Flower> flowers = new ArrayList<>();
StringTokenizer st;
for(int i=0; i < N; i++){
st = new StringTokenizer(br.readLine());
int start_m = Integer.parseInt(st.nextToken());
int start_d = Integer.parseInt(st.nextToken());
int end_m = Integer.parseInt(st.nextToken());
int end_d = Integer.parseInt(st.nextToken());
flowers.add(new Flower(start_m,start_d, end_m, end_d));
}
Collections.sort(flowers);
๋ค์์ผ๋ก ํ์ ๊ธฐ์ค์ด ๋๋ ๋ ์ง 3์ 1์ผ์ ๋ด์ ์ ์๋ ๋ณ์ currentMonth, currentDay๋ฅผ ์ ์ธํด ์ฃผ์๊ณ , ๊ฝ์ ์์ฐจ์ ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ ์ํ index ๋ณ์, ๊ฝ ๊ฐ์๋ฅผ ์๊ธฐ ์ํ count ๋ณ์๋ฅผ ์ ์ธํ๋ค.
int currentMonth = 3; int currentDay = 1; int index = 0; int count = 0; // ๊ฝ ๊ฐ์
๋ง์ง๋ง์ผ๋ก ์๋์ ๊ฐ์ด ์ฒ์ ์๊ฐํ๋ ๊ฝ์ ์ ํ๊ณผ์ ์ ๊ตฌํํ์๋ค. ์์์ผ์ด ํ์ฌ ๋ ์ง ์ดํ์ด๋ฉด์ ๊ฐ์ฅ ๋ฆ๊ฒ ์ง๋ ๊ฝ์ ์ ํํ๋ ๊ณผ์ ์ด๋ค. ์ฌ๊ธฐ์ isLaterDate ๋ฉ์๋๊ฐ ๊ฐ์ฅ ๋ฆ๊ฒ ์ง๋ ๊ฝ์ ๊ฐฑ์ ํด์ฃผ๋ ์ญํ ์ ํ๋๋ฐ ๋ง์ฝ ํด๋น ๋ฉ์๋๊ฐ ํ๋ฒ๋ ํธ์ถ์ด ๋์ง ์์๋ค๋ฉด ์์์ผ์ด ํ์ฌ ๋ ์ง ์ดํ์ด๋ฉด์ ์ง๋ ๋ ์ง๊ฐ ์์์ผ ์ดํ์ธ ๊ฝ์ด ํ๋๋ ์กด์ฌํ์ง ์๋๋ค๋ ์๋ฏธ์ด๋ฏ๋ก ๋ฌธ์ ์ ์กฐ๊ฑด์ ๋ง์กฑ์ํค์ง ์์ ์ข ๋ฃ์์ผ์ฃผ์ด์ผํ๋ค. ์ด๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด found ๋ณ์๋ฅผ ์ถ๊ฐํด ์ฃผ์๋ค.
๊ทธ๋ ๊ฒ ์ ํ๋ maxEndMoth, maxEndDay(์ข ๋ฃ์ผ์ด ๊ฐ์ฅ ๋ค์ธ ๊ฝ)์ currentMonth, currentDay๋ก ์ ๋ฐ์ดํธํ๊ณ ์ด๋ฅผ ๋ฐ๋ณตํ๋ค.
while (currentMonth < 12) {
int maxEndMonth = currentMonth;
int maxEndDay = currentDay;
boolean found = false;
while(index < flowers.size() &&
(flowers.get(index).start[0] < currentMonth ||
(flowers.get(index).start[0] == currentMonth &&
flowers.get(index).start[1] <= currentDay))){
Flower current = flowers.get(index);
//๋ ๋ฆ์ผ๋ฉด ๊ฐฑ์
if(isLaterDate(current.end[0], current.end[1], maxEndMonth, maxEndDay)){
maxEndMonth = current.end[0];
maxEndDay = current.end[1];
found = true;
}
index++;
}
if(!found){
System.out.println(0);
return;
}
currentMonth = maxEndMonth;
currentDay = maxEndDay;
count++;
}
System.out.println(count);
}
private static boolean isLaterDate(int m1, int d1, int m2, int d2){
if(m1 != m2){
return m1 > m2;
}
return d1 > d2;
}
๋๋์ :
์ด๋ฒ ๋ฌธ์ ๋ ์ ๊ทผ ๋ฐฉ๋ฒ๋ณด๋ค ๋์ ์๊ฐ์ ์ ํํ ์ฝ๋๋ก ๊ตฌํํ๋ ๊ณผ์ ์์ ์ค๋ ๊ฑธ๋ ธ๋๋ฐ, ์กฐ๊ฑด์ ๋ง์กฑ์ํค์ง ๋ชป ํ ๊ฒฝ์ฐ 0์ ์ถ๋ ฅํด์ผํ๋ ๊ฒฝ๊ณ ์กฐ๊ฑด๋ค์ ์ฒดํฌํด์ผ ํ๋ ์ ์ด ์ค์ํ๋ค. ๋ํ ์์ ๋ ์ง๋ฅผ ๊ธฐ์ค์ผ๋ก ๋์ดํ ๊ฒ์ด ๋งค ์ ํ๋ง๋ค ๋ชจ๋ ๊ฝ์ ํ์ธํ์ง ์๊ณ ํ์ฌ ๋ ์ง ์ดํ์ ํผ๋ ๊ฝ๋ค๋ง ๊ฒํ ํ ์ ์๊ฒ ํด ์ฃผ์๋ค.
๊ทธ๋ฆฌ๊ณ ๊ทธ ํ์ฌ ์์ ์์์ ์ต์ ์ ์ ํ์ด ์ ์ฒด์ ์ผ๋ก๋ ์ต์ ์ด ๋๋ ๊ตฌ์กฐ๋ก, ๋ค์ ๋ฐฑํธ๋ํนํ์ง ์์๋ ๋๋ ๊ทธ๋ฆฌ๋ ์๊ณ ๋ฆฌ์ฆ ํน์ฑ์ ๋ํ ์ดํด๋ ์ ํ ์ ์๊ฒ ๋ ๊ฒ ๊ฐ๋ค.