백준 1931번
https://www.acmicpc.net/problem/1931
그리디, 정렬 문제이다.
PriorityQueue를 활용해서 문제를 해결하였다.
정렬조거만 생각하면 사실 어려운 문제는 아님
import java.io.*;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
// input
private static BufferedReader br;
// variables
private static int N;
private static PriorityQueue<Meeting> pQue;
private static Meeting[] times;
private static class Meeting implements Comparable<Meeting> {
int startTime;
int endTime;
private Meeting(int startTime, int endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
@Override
public int compareTo(Meeting o) {
if (endTime == o.endTime) {
return startTime - o.startTime;
}
return endTime - o.endTime;
}
@Override
public String toString() {
return "Meeting{" +
"startTime=" + startTime +
", endTime=" + endTime +
'}';
}
} // End of Meeting class
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
input();
bw.write(solve());
bw.close();
} // End of main()
private static String solve() {
StringBuilder sb = new StringBuilder();
int count = 0;
int time = 0;
while (!pQue.isEmpty()) {
Meeting tmp = pQue.poll();
if (tmp.startTime >= time) {
time = tmp.endTime;
count++;
}
}
sb.append(count);
return sb.toString();
} // End of solve()
private static void input() throws IOException {
N = Integer.parseInt(br.readLine());
pQue = new PriorityQueue<>();
times = new Meeting[N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int start = Integer.parseInt(st.nextToken());
int end = Integer.parseInt(st.nextToken());
pQue.offer(new Meeting(start, end));
}
} // End of input()
} // End of Main class