๊ธฐ๋ก๋ฐฉ

BOJ_1015 : ์ˆ˜์—ด ์ •๋ ฌ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1015 : ์ˆ˜์—ด ์ •๋ ฌ

Soom_1n 2022. 10. 19. 17:35

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

 

1015๋ฒˆ: ์ˆ˜์—ด ์ •๋ ฌ

P[0], P[1], ...., P[N-1]์€ 0๋ถ€ํ„ฐ N-1๊นŒ์ง€(ํฌํ•จ)์˜ ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ์ˆ˜์—ด์ด๋‹ค. ์ˆ˜์—ด P๋ฅผ ๊ธธ์ด๊ฐ€ N์ธ ๋ฐฐ์—ด A์— ์ ์šฉํ•˜๋ฉด ๊ธธ์ด๊ฐ€ N์ธ ๋ฐฐ์—ด B๊ฐ€ ๋œ๋‹ค. ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ B[P[i]] = A[i]์ด๋‹ค. ๋ฐฐ์—ด A๊ฐ€ ์ฃผ

www.acmicpc.net



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

  • ์ž…๋ ฅ๋œ ๋ฐฐ์—ด์˜ ์ˆซ์ž๋“ค์ด ์ •๋ ฌ๋์„๋•Œ ์–ด๋Š ์ธ๋ฑ์Šค๋กœ ๊ฐ€๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

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

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

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

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

        Arrays.sort(b);

        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                if (a[i] == b[j]){
                    System.out.print(j + " ");
                    b[j] = -1;
                    break;
                }
            }
        }
    }
}

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

  • ์ž…๋ ฅ๋œ ๋ฐฐ์—ด์„ intํ˜• ๋ฐฐ์—ด a์— ์ €์žฅํ•œ๋‹ค.
    • ๋™์‹œ์— ๋ฐฐ์—ด b์˜ ๊ฐ™์€ ์ธ๋ฑ์Šค์— ์ €์žฅํ•œ๋‹ค.
    • a๋Š” ์ฒ˜์Œ ์ƒํƒœ๋ฅผ ์ €์žฅํ•ด๋‘๊ธฐ ์œ„ํ•œ ๋ฐฐ์—ด์ด๊ณ , b๋Š” ์ •๋ ฌ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • a์™€ b์˜ ์›์†Œ๋ฅผ ๋น„๊ตํ•˜๋ฉฐ, ๊ฐ™์œผ๋ฉด ํ˜„์žฌ ์ธ๋ฑ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ ๊ฐ’์€ -1๋กœ ๋ฐ”๊ฟ” ์ค‘๋ณต๋˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ํ’€๊ณ ๋‚˜๋‹ˆ ๊ฐ„๋‹จํ–ˆ์ง€๋งŒ, ๋ฌธ์ œ์˜ ๋ฐฐ์—ด ๋ณ€ํ™˜์ด ์–ด๋–ค ์˜๋ฏธ์ธ์ง€ ์ดํ•ด๋˜์ง€ ์•Š์•„์„œ ์กฐ๊ธˆ ํ—ค๋งค๊ฒŒ ๋˜์—ˆ๋‹ค.

728x90