๊ธฐ๋ก๋ฐฉ

BOJ_12015 : ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด 2 ๋ณธ๋ฌธ

CodingTest/Java

BOJ_12015 : ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด 2

Soom_1n 2024. 4. 9. 20:23

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

12015๋ฒˆ: ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด 2

์ฒซ์งธ ์ค„์— ์ˆ˜์—ด A์˜ ํฌ๊ธฐ N (1 ≤ N ≤ 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ˆ˜์—ด A๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ๋Š” Ai๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Ai ≤ 1,000,000)

www.acmicpc.net

 


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ฃผ์–ด์ง„ ์ˆ˜์—ด A์˜ ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด (LIS)์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • n์ด 100๋งŒ์ด๋ฏ€๋กœ LIS์˜ ๊ฐ„๋‹จํ•œ ํ’€์ด ๋ฒ•์ธ DP๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, O(n^2)์œผ๋กœ 1์กฐ๋กœ ์‹œ๊ฐ„์„ ์ดˆ๊ณผํ•œ๋‹ค.
  • LIS์˜ ๋‘ ๋ฒˆ์งธ ํ’€์ด๋ฒ•์ธ ์ด๋ถ„ ํƒ์ƒ‰์„ ์ด์šฉํ•ด O(nlogn)์œผ๋กœ ํ’€์ดํ•œ๋‹ค. (600๋งŒ)
    • LIS ๋ฐฐ์—ด์„ ์ž…๋ ฅ ๋ฐฐ์—ด ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊ณจ๋ผ ์ฑ„์›Œ๊ฐ„๋‹ค.
    • ์ž…๋ ฅ ๋ฐฐ์—ด์˜ ์›์†Œ๋ฅผ ์ฑ„์šธ ์ž๋ฆฌ๋Š” ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ๊ตฌํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

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 N = Integer.parseInt(br.readLine());
        int[] A = new int[N];

        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(st.nextToken());
        }

        int[] LIS = new int[N];
        int len = 1;
        LIS[0] = A[0];

        for (int i = 1; i < N; i++) {
            if (LIS[len - 1] < A[i]) { // ์ •๋‹ต ๋ฐฐ์—ด ๋’ค์— ์ถ”๊ฐ€
                LIS[len++] = A[i];
            } else if (LIS[len - 1] > A[i]) { // ๋“ค์–ด๊ฐˆ ์ž๋ฆฌ ์ด์ง„ ํƒ์ƒ‰
                int mid = len / 2;
                int move = mid / 2;

                while (true) {
                    if (mid == 0) {
                        break;
                    } else if (LIS[mid - 1] < A[i] && A[i] <= LIS[mid]) {
                        break;
                    } else if (A[i] < LIS[mid]) { // ์™ผ์ชฝ ํƒ์ƒ‰
                        mid -= move;
                    } else { // ์˜ค๋ฅธ์ชฝ ํƒ์ƒ‰
                        mid += move;
                    }
                    move = Math.max(1, move / 2);
                }
                LIS[mid] = A[i];
            }
        }
        bw.write(Integer.toString(len));
        bw.flush();
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๋ฐฐ์—ด LIS์— ์›์†Œ๋ฅผ ์ฑ„์›Œ๊ฐ€๋ฉฐ ์ •๋‹ต์„ ๊ตฌํ•œ๋‹ค.
    • LIS์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ LIS[len - 1]์ด ์ž…๋ ฅ ๋ฐฐ์—ด์˜ ํ˜„์žฌ ์›์†Œ A[i]๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ๋ฐ”๋กœ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— A[i]๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
    • A[i]๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด, LIS ์ค‘๊ฐ„์— ๋“ค์–ด๊ฐˆ ์ž๋ฆฌ๋ฅผ ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ๊ตฌํ•œ๋‹ค.
  • ์ด๋ถ„ ํƒ์ƒ‰์˜ ๋ชฉ์ ์€ LIS[mid-1] < A[i] <= LIS[mid] ์— ํ•ด๋‹นํ•˜๋Š” mid๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค.
    • mid == 0 ์ผ ๋•Œ, LIS[mid-1] ๊ฐ€ ์˜ค๋ฅ˜๋‚˜๋ฏ€๋กœ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • ์ตœ์ข… ๊ตฌํ•ด์ง„ LIS ๋ฐฐ์—ด์˜ ๊ธธ์ด len์„ ์ถœ๋ ฅํ•œ๋‹ค.
    • LIS ๋ฐฐ์—ด์ด ์ •ํ™•ํ•œ ์ •๋‹ต ๋ฐฐ์—ด์€ ์•„๋‹ˆ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • LIS์˜ DP ํ’€์ด๋งŒ ์•Œ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ, ์ด๋ถ„ ํƒ์ƒ‰ ํ’€์ด์— ๋Œ€ํ•ด์„œ ์•Œ๊ฒŒ๋˜์—ˆ๊ณ  ํฌ์ŠคํŒ…์œผ๋กœ ์ •๋ฆฌํ–ˆ๋‹ค.
    • LIS๊ด€๋ จ ๋ฌธ์ œ๋Š” ์•„์ง ๋งŽ๊ณ , ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ํ’€์—ˆ์„๋•Œ ์›์†Œ ๋ชฉ๋ก์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ๋„ ํ’€์–ด๋ณด์ง€ ์•Š์•„์„œ ๋” ์—ฐ์Šตํ•ด์•ผ๊ฒ ๋‹ค.
  • mid๊ฐ€ 0 ์ผ ๋•Œ์˜ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์•„ 1ํšŒ ํ‹€๋ ธ๋‹ค.

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_2143 : ๋‘ ๋ฐฐ์—ด์˜ ํ•ฉ  (0) 2024.04.10
BOJ_1644 : ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ  (0) 2024.04.09
BOJ_1005 : ACM Craft  (0) 2024.04.04
BOJ_12100 : 2048 (Easy)  (0) 2024.03.31
BOJ_20040 : ์‚ฌ์ดํด ๊ฒŒ์ž„  (0) 2024.03.29