๊ธฐ๋ก๋ฐฉ

BOJ_1517 : ๋ฒ„๋ธ” ์†ŒํŠธ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1517 : ๋ฒ„๋ธ” ์†ŒํŠธ

Soom_1n 2024. 1. 26. 17:02

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

 

1517๋ฒˆ: ๋ฒ„๋ธ” ์†ŒํŠธ

์ฒซ์งธ ์ค„์— N(1 ≤ N ≤ 500,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ ์ค„์—๋Š” N๊ฐœ์˜ ์ •์ˆ˜๋กœ A[1], A[2], …, A[N]์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ A[i]๋Š” 0 ≤ |A[i]| ≤ 1,000,000,000์˜ ๋ฒ”์œ„์— ๋“ค์–ด์žˆ๋‹ค.

www.acmicpc.net



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

  • N๊ฐœ์˜ ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆ˜์—ด์„ ๋ฒ„๋ธ” ์†ŒํŠธ๋กœ ์ •๋ ฌํ–ˆ์„ ๋•Œ, swap์ด ์ด ๋ช‡ ๋ฒˆ ๋ฐœ์ƒํ•˜๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

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

  • N์ด 50๋งŒ๊นŒ์ง€ ๊ฐ€๋ฏ€๋กœ O(nlogn)์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
    • ์—ฌ๊ธฐ์„œ๋Š” ๋ณ‘ํ•ฉ ์ •๋ ฌ(Merge Sort)์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ฒ„๋ธ” ์†ŒํŠธ๋Š” 1์นธ์”ฉ ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ •๋ ฌ ํ›„ ๋ฐ”๋€ ์ธ๋ฑ์Šค์˜ ํฌ๊ธฐ๋ฅผ ๋ชจ๋‘ ๋”ํ•˜๋ฉด ๋œ๋‹ค.
    • ๋ณ‘ํ•ฉ ์ •๋ ฌ์—์„œ ๊ธฐ์กด ์ธ๋ฑ์Šค๋ณด๋‹ค ์•ž ์ธ๋ฑ์Šค๋กœ ์˜ฎ๊ธธ ๋•Œ, ๊ทธ ํฌ๊ธฐ๋ฅผ ๋ˆ„์ ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.

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

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

public class Main {

    private static int[] arr, temp;
    private static long result;

    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());
        arr = new int[N];
        temp = new int[N];

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

        result = 0;
        merge_sort(0, N - 1);

        bw.write(Long.toString(result));
        bw.flush();
    }

    private static void merge_sort(int start, int end) {
        if (end - start < 1) return;

        int mid = start + (end - start) / 2;
        int tempIdx = start;
        int left = start;
        int right = mid + 1;

        merge_sort(left, mid);
        merge_sort(right, end);

        while (left <= mid && right <= end) {
            if (arr[left] <= arr[right]) {
                temp[tempIdx++] = arr[left++];
            } else {
                temp[tempIdx++] = arr[right++];
                result += right - tempIdx;
            }
        }

        while (left <= mid) {
            temp[tempIdx++] = arr[left++];
        }
        while (right <= end) {
            temp[tempIdx++] = arr[right++];
        }

        tempIdx = start;
        
        for (int i = start; i <= end; i++) {
            arr[i] = temp[tempIdx++];
        }
    }
}

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

  • ๊ธฐ๋ณธ์ ์ธ ๋ณ‘ํ•ฉ์ •๋ ฌ์„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ, ์ธ๋ฑ์Šค ์ด๋™์„ result์— ์นด์šดํŠธํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.
    • ์•ž๋’ค ์ด๋™ ๋ชจ๋‘ ์„ธ๋ฉด ์ค‘๋ณต๋˜๋ฏ€๋กœ, ์ธ๋ฑ์Šค๊ฐ€ ์•ž์œผ๋กœ ์ด๋™ํ•  ๋•Œ๋งŒ ์นด์šดํŠธํ–ˆ๋‹ค.
    • left ํ˜น์€ right์—์„œ ๋‚จ์€ ์›์†Œ๋“ค์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์€ ์ธ๋ฑ์Šค๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ ํฌ๋ฏ€๋กœ, result๋ฅผ ์นด์šดํŠธํ•˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ณ‘ํ•ฉ ์ •๋ ฌ์„ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š”๊ฑด ์ฒ˜์Œ์ธ๋ฐ, ์‚ฌ์†Œํ•œ ๋ถ€๋ถ„๋“ค์„ ์‹ค์ˆ˜ํ•œ๊ฒŒ ๋„ˆ๋ฌด ๋งŽ์•˜๋‹ค.
    • merge_sort()๋ฉ”์„œ๋“œ์—์„œ start์™€ end๋กœ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•ด์•ผ ํ•˜๋Š”๋ฐ, start ๋Œ€์‹  0์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค.
    • left, right์˜ ๋‚จ์€ ์›์†Œ๋“ค์„ temp๋กœ ๋ณต์‚ฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋ˆ„๋ฝํ–ˆ์—ˆ๋‹ค.
    • result๋ฅผ ์นด์šดํŠธํ•˜๋Š” ๋ถ€๋ถ„์„ ์ธ๋ฑ์Šค๊ฐ€ ์ปค์ง€๋Š”์ชฝ๋„ ํฌํ•จ์‹œ์ผœ์„œ ์ค‘๋ณต ์นด์šดํŠธ๊ฐ€ ๋˜์—ˆ๋‹ค.
  • ๊ทธ๋ž˜๋„ ์‚ฌ์†Œํ•œ ๋ถ€๋ถ„๋“ค์„ ๋‹ค ํ‹€๋ ค๋ณด์•„์„œ, ๋ณ‘ํ•ฉ ์ •๋ ฌ์— ๋Œ€ํ•œ ๊ณต๋ถ€๊ฐ€ ์ž˜ ๋œ ๊ฒƒ ๊ฐ™๋‹ค.
  • ์„ธ๊ทธ๋จผํŠธ ํŠธ๋ฆฌ๋กœ๋„ ํ’€์ด๊ฐ€ ๋˜์–ด ๋ณด์ด๋Š”๋ฐ, ๋‚˜์ค‘์— ๋‹ค์‹œ ํ’€์–ด๋ณผ ์ƒ๊ฐ์ด๋‹ค.

 

728x90