1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 1


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/1946


image


๐Ÿ“Œ ํ’€์ด

์ง€๊ธˆ๊นŒ์ง€ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ๋‚œ์ด๋„๋ฅผ ๋ณด๊ณ  ์ซ„๋ฆฌ๋Š” ๊ฒฝํ–ฅ์ด ์žˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ํ’€๋ ค์•ผ์ง€ ๋‚œ์ด๋„ ๊ณต๊ฐœ๊ฐ€ ๋˜๊ฒŒ ์„ค์ •์„ ํ•˜์˜€๋‹ค.
์ด ๋ฌธ์ œ๋Š” ์„ค๋ช…์ด ๋˜๊ฒŒ ์–ด๋ ค์› ๋‹ค.

ํ•˜์ง€๋งŒ, ์•ž์˜ ์ˆซ์ž๋ฅผ ์ž‘์€์ˆ˜๋ถ€ํ„ฐ ์ •๋ ฌํ•œ ํ›„, ๋’ค์˜ ์ˆซ์ž๊ฐ€ ์ž‘์•„์งˆ๋•Œ๋งˆ๋‹ค result๋ฅผ +1 ํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค.
์ฒ˜์Œ์—๋Š” ๋‹จ์ˆœํžˆ ์ •๋ ฌ์„ ์“ฐ๋ ค๊ณ  ํ–ˆ์ง€๋งŒ, ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋ฌธ์ œ๊ฐ€ ๋  ๊ฒƒ ๊ฐ™์•„์„œ, ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋†“๊ณ , ์•ž์˜ ๊ฐ’์˜ ์ฃผ์†Œ๊ฐ’์— ๋’ค์˜ ๊ฐ’์„ ๋„ฃ์€ํ›„, ๋” ์ž‘์•„์ง€๋ฉด +1์„ ํ•ด์ฃผ๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

๋‹ค์Œ์€ ๊ทธ ์ฝ”๋“œ์ด๋‹ค.


๐Ÿ“Œ Code

package BOJ.Greedy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class N1946 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        int T = Integer.parseInt(br.readLine());

        for (int i = 0; i < T; i++) {
            int N = Integer.parseInt(br.readLine());
            int[] tc = new int[N];
            for (int j = 0; j < N; j++) {
                st = new StringTokenizer(br.readLine());
                tc[Integer.parseInt(st.nextToken()) - 1] = Integer.parseInt(st.nextToken());
            }
            int max = tc[0];
            int result = 1;
            for (int j = 0; j < N; j++) {

                if (tc[j] < max) {
                    max = tc[j];
                    result += 1;
                }
            }
            System.out.println(result);
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜