Notice
Recent Posts
Recent Comments
Today
Total
05-21 20:46
Link
관리 메뉴

Partially Committed

[λ°±μ€€ 1931] νšŒμ˜μ‹€ λ°°μ • λ³Έλ¬Έ

πŸ”₯ Algorithm || λ¬Έμ œν’€μ΄/PS

[λ°±μ€€ 1931] νšŒμ˜μ‹€ λ°°μ •

WonderJay 2023. 7. 28. 13:57
728x90
λ°˜μ‘ν˜•
SMALL

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

 

1931번: νšŒμ˜μ‹€ λ°°μ •

(1,4), (5,7), (8,11), (12,14) λ₯Ό μ΄μš©ν•  수 μžˆλ‹€.

www.acmicpc.net


μ‚΄λ©΄μ„œ μ½”ν…Œμ— μ‘μ‹œν•œ 적은 λͺ‡λ²ˆ μ—†μ§€λ§Œ

 

이 νšŒμ˜μ‹€ λ°°μ • μœ ν˜•μ΄ 자꾸 λ‚˜μ˜€λŠ” 것 κ°™μ•„μ„œ 이번 κΈ°νšŒμ— κ΄€λ ¨ μœ ν˜•μ„ λͺ¨μ‘°λ¦¬ μ •λ¦¬ν•΄λ³΄λ €ν•œλ‹€.

 

1931 νšŒμ˜μ‹€ λ°°μ • λ¬Έμ œλŠ” κ·Έ μ€‘μ—μ„œλ„ κ°€μž₯ 기본적인 λ¬Έμ œμ΄λ‹€.

 

문제 상황은 λ‹¨μˆœν•˜λ‹€.

 

νšŒμ˜μ‹€μ΄ ν•˜λ‚˜ μ‘΄μž¬ν•˜κ³ ,

 

μ‹œμž‘μ‹œκ°„, λμ‹œκ°„μœΌλ‘œ 이루어진 회의 μ‹œκ°„ν‘œλ“€μ΄ N 개 μ£Όμ–΄μ§ˆ λ•Œ

 

μ΅œλŒ€ν•œ 많이 진행할 수 μžˆλŠ” 회의의 개수λ₯Ό κ΅¬ν•˜λ©΄ λœλ‹€.

 

즉,

 

νšŒμ˜μ‹€μ΄ μ‚¬μš©λ˜μ§€ μ•ŠλŠ” μ‹œκ°„μ„ μ΅œμ†Œν™”ν•΄μ•Ό ν•œλ‹€λŠ” 것이닀.

 

μ–΄λ–»κ²Œ ν’€ 수 μžˆμ„κΉŒ..?

 

생각해보면 μ‰½κ²Œ λ– μ˜¬λ¦΄ 수 μžˆλ‹€.

 

κ·Έλƒ₯!

 

회의 μ’…λ£Œ μ‹œκ°„μ΄ λΉ λ₯Έ 순으둜 λ°°μ •ν•˜λ©΄ λœλ‹€.

 

예제 상황을 톡해 μƒκ°ν•΄λ³΄μž.

 

 

예제 1 μΌ€μ΄μŠ€λŠ” 이미 μ •λ ¬λœ μƒνƒœλ‘œ 주어진닀.

 

그럼 μ •λ ¬ν–ˆλ‹€κ³  치고,

 

맨 μ²˜μŒμ—λŠ” 

 

(1, 4) λ₯Ό λ°°μ •ν•  것이닀.

 

κ·Έ λ‹€μŒμ—λŠ”?

 

회의 리슀트λ₯Ό νƒμƒ‰ν•˜λ©΄μ„œ,

 

이전에 회의의 λμ‹œκ°„ 이후에 μ‹œμž‘ν•˜λŠ” 회의λ₯Ό λ°°μ •ν•˜λ©΄ λœλ‹€.

 

μœ„ κ²½μš°μ—λŠ” (5,7) 이 ν•΄λ‹Ήν•  것이닀. 

 

κ·Έ λ‹€μŒμ€ μ•„λ¬΄λž˜λ„ 8, 11 이 μ μ ˆν•  것이고

 

κ·Έ λ‹€μŒμ€ 12, 14 λ₯Ό λ°°μ •ν•΄μ•Ό ν•  것이닀.

 

μ΅œμ’… 좜λ ₯은 회의λ₯Ό λ°°μ •ν•œ 개수λ₯Ό λ°˜ν™˜ν•˜λ©΄ λœλ‹€.

 

회의λ₯Ό λ°°μ •ν•œ 개수λ₯Ό λ°˜ν™˜ν•˜κΈ° μœ„ν•΄μ„œλŠ”,

 

λ³€μˆ˜λ₯Ό ν•˜λ‚˜ 두고 계속 μΉ΄μš΄νŒ…μ„ 해도 λ˜μ§€λ§Œ

 

λ‚˜λŠ” stack 을 λ§Œλ“€μ–΄λ‘κ³ 

 

stack 에 "이전 회의의 λλ‚˜λŠ” μ‹œκ°„" 을 push ν•˜λŠ” 방법을 μ‚¬μš©ν–ˆλ‹€.

 

μ΄λ ‡κ²Œν•˜λ©΄ stack 의 size κ°€ 닡이 λœλ‹€.

 

μ½”λ“œλŠ” μ•„λž˜μ™€ κ°™λ‹€.

 

import java.io.*;
import java.util.ArrayList;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk;
        Stack<Integer> st = new Stack<>();
        ArrayList<int[]> table = new ArrayList<>();
        int n = Integer.parseInt(br.readLine());

        for (int i = 0; i < n; i++) {
            stk = new StringTokenizer(br.readLine());
            int s = Integer.parseInt(stk.nextToken());
            int e = Integer.parseInt(stk.nextToken());
            table.add(new int[]{s, e});
        }

        // λλ‚˜λŠ” μ‹œκ°„μ΄ λΉ λ₯Έ 순으둜 μ •λ ¬
        table.sort((int[] a, int[] b) -> {
            if (a[1] == b[1]) {
                return a[0] - b[0];
            }
            return a[1] - b[1];
        });

        for (int i = 0; i < table.size(); i++) {
            int[] now = table.get(i);
            if (st.isEmpty()) {
                st.add(now[1]);
            } else if (now[0] >= st.peek()) {
                st.add(now[1]);
            }
        }
        System.out.println(st.size());

        bw.flush();
        bw.close();
    }
}

 

728x90
λ°˜μ‘ν˜•
LIST
Comments