๋ฌธ์
ํด๋น ํฌ์คํ ์ ๋ฐฑ์ค์ ๋ฌธ์ ์ด๋ฆ ์ ์ ๊ทผ๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ค๋ช ํ ๊ธ ์ ๋๋ค.
์ ๋ต ์์ค ์ฝ๋๋ฅผ ํ์ธํ์๋ ค๋ฉด solve url ์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค.
์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ด๋ค ๋ฐฉ์์ผ๋ก ์ ๊ทผํด์ผ ํ๋์ง๋ฅผ ๋จผ์ ์๊ฐํด๋ณด์.
๋ฌธ์ ์ ๊ทผ
์ด๋ฒ ๋ฌธ์ ๋ Greedy ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด์ ํด๊ฒฐํ ์ ์๋ ๋ฌธ์ ์ด๋ค.
Greedy ์๊ณ ๋ฆฌ์ฆ์ ํน์ฑ๋ต๊ฒ ํต์ฌ ์์ด๋์ด๊ฐ ์ค์ํ ๋ฌธ์ ๊ฐ๋ค.
ํด๊ฒฐ๋ฒ
๋ง์ฝ ์๋ฅ ์ ์๋ ๋ฉด์ ์ด๋ ํ๋๋ผ๋ 1๋ฑ์๊ฒ ๋จ์ด์ง๋ฉด ๊ทธ ์ธ์์ ์ ๋๋ก ์ ์ฌํ ์ ์๋ค.
์ฆ, ์๋ฅ 1๋ฑ๋ณด๋ค ๋ฉด์ ์ ์๊ฐ ๋ฎ์ ์ง์์๋ ๋ชจ๋ ๋จ์ด์ง๊ฒ ๋๋ค.
์ด์ ๋ ์๋ฅ 1๋ฑ์ ๋ฉด์ ๋ฑ์๊ฐ 4๋ฑ์ด๋ผ๋ฉด, ๋ค๋ฅธ ์ง์์๋ ๋ฌด์กฐ๊ฑด ์๋ฅ๋ 1๋ฑ๋ณด๋ค ๋ฎ๊ฒ ๋๋๋ฐ, ๋ฉด์ ๋ง์ ๋ 1๋ฑ๋ณด๋ค ๋ชปํ ๋ฑ์๊ฐ ๋์ค๋ฉด 1๋ฑ๋ณด๋ค ํญ์ ๋ค์ณ์ง๊ธฐ ๋๋ฌธ์ ํ๋ฝํ๊ฒ ๋๋ค.
์ด๋ฅผ ์ด์ฉํด์ ์๋ฅ ๋ฑ์๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ์ ์ํํ๊ณ , ์๋ฅ 2๋ฑ ๋ถํฐ ๋ฉด์ ์ ์๊ฐ ์๋ฅ 1๋ฑ์ ์ ์๋ณด๋ค ๋์ผ๋ฉด ์ฑ์ฉํ๋ฉด ๋๋ค.
๋์ ๋งค๋ฒ ๋ฉด์ ์ ๋ฑ์๋ฅผ ์ปคํธ๋ผ์ธ์ผ๋ก ๊ฐฑ์ ์์ผ์ค์ผํ๋ค.
if(applicants[i].interview < cutLine) {
count++;
cutLine = applicants[i].interview;
}
์ ๋ต ์ฝ๋
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
int n = Integer.parseInt(br.readLine());
Applicant[] applicants = new Applicant[n];
for (int i = 0; i < n; i++) {
int[] s = Arrays.stream(br.readLine().split(" "))
.mapToInt(Integer::parseInt).toArray();
applicants[i] = new Applicant(s[0], s[1]);
}
Arrays.sort(applicants, (a, b) -> a.paper - b.paper);
int cutLine = applicants[0].interview;
int count = 1;
for (int i = 1; i < applicants.length; i++) {
if(applicants[i].interview < cutLine) {
count++;
cutLine = applicants[i].interview; // ???
}
}
bw.write(count + "\n");
}
bw.flush();
bw.close();
}
private static class Applicant {
int paper;
int interview;
public Applicant(int paper, int interview) {
this.paper = paper;
this.interview = interview;
}
}
}
๋๊ธ