2 minute read

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

๐Ÿ’ง Platinum 5


๐Ÿ“Œ ๋ฌธ์ œ

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


image

image


๐Ÿ“Œ ํ’€์ด

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ ์Šฌ์Šฌ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ๊ฐ์ด ์žกํžˆ๊ธฐ ์‹œ์ž‘ํ•˜๊ณ , ๊ณจ๋“œ๊ฐ€ ํ’€๋ฆฌ๊ธฐ ์‹œ์ž‘ํ•ด์„œ ๊ธฐ๋…๋น„์ ์ธ ๋Š๋‚Œ์œผ๋กœ ํ”Œ๋ ˆํ‹ฐ๋„˜ ๋ฌธ์ œ๋ฅผ ๋„์ „ํ•ด๋ณด์•˜๋‹ค.

์ฒ˜์Œ ์ด ๋ฌธ์ œ๋ฅผ ๋ณด์ž๋งˆ์ž ํ•œ ์ƒ๊ฐ์€ ์ผ๋‹จ ์ •๋ ฌ์€ ํ•ด๋ณด์ž๋ผ๋Š” ์ƒ๊ฐ์ด์˜€๋‹ค.
๊ทธ๋ž˜์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ณ„์‚ฐํ•ด๋ณด๋‹ˆ ๋‹จ์ˆœํ•˜๊ฒŒ Arrays.sort ๋ฅผ ์‚ฌ์šฉํ•ด๋„ o(n^2) = 1์–ต โ‰’ 1์ดˆ๋ผ ์ •๋ ฌ์„ ํ•˜๊ณ  ์‹œ์ž‘ํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  n๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์˜ค๋ฉด nํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ์ˆซ์ž๋ฅผ ์ฑ„์›Œ๋„ฃ์—ˆ๋‹ค.
์ด๋•Œ ๋‹ค์Œ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ์ˆซ์ž๊ฐ€ ์ด์ „ ๊ฐ’+1์ด๋ผ๋ฉด int index๋ผ๋Š” ๋ณ€์ˆ˜์— ๊ทธ ์ฃผ์†Œ๊ฐ’์„ ์ง‘์–ด๋„ฃ์€ํ›„, ๊ทธ ๋‹ค์Œ ์œ„์น˜์— ์ˆซ์ž๋ฅผ ์ง‘์–ด๋„ฃ์—ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  index๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ final int notExist = 10001 ๋ผ๋Š” ์ƒ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์ฃผ์—ˆ๋Š”๋ฐ, ๋งŒ์•ฝ ์ˆซ์ž๋ฅผ ๋„ฃ๊ธฐ ์ „์— index ๊ฐ’์ด notExist๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด index ์ด์ „ ์ฃผ์†Œ์— ์žˆ๋Š” ๊ฐ’๊ณผ ๋น„๊ตํ•ด์„œ ์ด์ „ ์ฃผ์†Œ์˜ ๊ฐ’ + 1 ์ด ์•„๋‹ˆ๋ผ๋ฉด ๋„ฃ์œผ๋ฉด์„œ index ๋ฅผ notExist ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ , ๊ฐ™์„์‹œ ๋’ค์— ์ถ”๊ฐ€๋กœ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜์˜€๋‹ค.

image

ํ•˜์ง€๋งŒ ์ด๋Ÿฐ์‹์˜ ํ’€์ด์—๋Š” ์•„์ง ํ•œ๊ฐ€์ง€ ๋ฐ˜๋ก€๊ฐ€ ๋‚จ์•„์žˆ์—ˆ๋‹ค.
๊ทธ ๋ฐ˜๋ก€๋Š” ๋ฐ”๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด index์— ๊ฐ’์ด ์ฑ„์›Œ์ง€์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์ข…๋ฃŒ๋˜๋Š” ๊ฒƒ์ด์˜€๋‹ค.

image

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋งŒ์•ฝ index != notExist ์ด๋ฉด ๊ฐ€์žฅ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์ฃผ์†Œ์˜ ์ˆซ์ž - 1 ๊ฐ€ ๋‚˜์˜ค๋Š” ์ฃผ์†Œ๊ฐ’์„ ์ฐพ์•„์„œ index์™€ ๋นผ์ฃผ๊ณ , ๊ทธ ์ฃผ์†Œ๊ฐ’๋ถ€ํ„ฐ i๊นŒ์ง€ ๋งˆ์ง€๋ง‰ ์ฃผ์†Œ์˜ ์ˆซ์ž๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ , index๋ถ€ํ„ฐ๋Š” ๊ทธ ์ˆซ์ž์— -1๋ฅผ ํ•œ๊ฐ’์„ ๋๊นŒ์ง€ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
์ดํ›„ ์ถœ๋ ฅ์€ ๋งˆ์ง€๋ง‰ ์ฃผ์†Œ - 1 ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋‹ˆ ๋‹ต์ด ๋‚˜์™”๋‹ค.

ํ”Œ๋ ˆํ‹ฐ๋„˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๋Š๋‚€ ์ ์€ ์ƒ๊ฐ๋ณด๋‹ค ๊ทธ๋ ‡๊ฒŒ ์–ด๋ ต์ง€๋Š” ์•Š๊ณ , ์ œ๋Œ€๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํŒŒ์•…ํ•œ ํ›„ ํ’€๋ฉด ์ƒ๊ฐ๋ณด๋‹ค ์‰ฝ๊ฒŒ ํ’€๋ฆฐ๋‹ค๋Š” ๊ฒƒ์ด์˜€๋‹ค.

์ฒ˜์Œ์œผ๋กœ ํ’€์–ด๋ณธ ํ”Œ๋ ˆํ‹ฐ๋„˜ ๋ฌธ์ œ์—ฌ์„œ ํ–‰๋ณตํ–ˆ๋‹ค. ๐Ÿ˜Ž


๐Ÿ“Œ Code

package BOJ.Greedy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class N1071 {
    static int[] input;
    static int[] output;
    static final int notExist = 10001;

    static int N;


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        N = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine());
        input = new int[N];
        output = new int[N + 1];
        Arrays.fill(output, notExist);
        for (int i = 0; i < N; i++) {
            input[i] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(input);
        SortDict();
    }

    static void SortDict() {
        int index = notExist;
        int curindex = 0;
        if (N != 0) {
            output[curindex++] = input[0];
        }
        for (int i = 1; i < N; i++) {
            if (index == notExist) {
                if (output[curindex - 1] + 1 != input[i]) {
                    output[curindex] = input[i];
                    curindex++;
                } else {
                    index = curindex++;
                    output[curindex++] = input[i];
                }
            } else {
                if (output[index - 1] + 1 != input[i]) {
                    output[index] = input[i];
                    index = notExist;
                } else {
                    output[curindex++] = input[i];
                }
            }
        }
        if (index != notExist) {
            int flag = index - 1;
            while (flag >= 0 && output[flag] + 1 == output[N]) {
                flag--;
            }
            flag++;

            int count = index - flag;
            for (int i = index + 1; i < N + 1; i++) {
                output[flag++] = output[N];
            }
            for (int i = 0; i < count; i++) {
                output[flag++] = output[N] - 1;
            }
        }
        ShowAll();
    }

    static void ShowAll() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < N; i++) {
            sb.append(output[i] + " ");
        }
        System.out.println(sb);
    }

    static void ShowTest() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < N + 1; i++) {
            if (output[i] == notExist) {
                sb.append(". ");
            } else {
                sb.append(output[i] + " ");
            }
        }
        System.out.println(sb);
    }
}

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



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