01
04

 

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

 

10431๋ฒˆ: ์ค„์„ธ์šฐ๊ธฐ

์ดˆ๋“ฑํ•™๊ต ์„ ์ƒ๋‹˜ ๊ฐ•์‚ฐ์ด๋Š” ์•„์ด๋“ค์„ ๋ฐ๋ฆฌ๊ณ  ๋‹จ์ฒด๋กœ ์–ด๋–ค ์ผ์„ ํ•  ๋•Œ ๋ถˆํŽธํ•จ์ด ์—†๋„๋ก ์ƒˆ๋กœ ๋ฐ˜์— ๋ฐฐ์ •๋ฐ›์€ ์•„์ด๋“ค์—๊ฒŒ ํ‚ค ์ˆœ์„œ๋Œ€๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค. ๋ฒˆํ˜ธ๋ฅผ ๋ถ€์—ฌํ•  ๋• ํ‚ค๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์•„์ด๊ฐ€ 1

www.acmicpc.net

 

import java.util.Scanner;

class Main{
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        int n = 20;
        for(int tc = 0; tc<T; tc++){
            int tcNum = sc.nextInt();
            int[] input = new int[n]; 
            int fallBack = 0;
            input[0] = sc.nextInt(); // ์ฒซ ๊ฐ’์€ ๊ทธ๋Œ€๋กœ ๋“ค์–ด๊ฐ
            for(int i=1; i<n;i++){
                input[i] = sc.nextInt();
                for(int j=0; j<i; j++){
                    if(input[j] > input[i]){
                        for(int k=i; k>j; k--){
                            input[k] = input[k-1];
                            fallBack++;
                        }
                        input[j] = input[i];
                    }
                }
            }
            System.out.println(tcNum+" "+ fallBack);
        }

        sc.close();
    }
}

์˜ค๋‹ต๋…ธํŠธ(ํ‘ธ๋Š” ๋™์•ˆ ์‚ฌ๊ณ ๊ณผ์ • ๋ฐ ๊ฐœ์„ ์ )

์‚ฝ์ž…์ •๋ ฌ์„ ์ƒ๊ฐํ•˜๋ฉด์„œ ํ’€์—ˆ๋‹ค. ์‚ฝ์ž…์‹œ, ์ด์ „ ๋ฐฐ์—ด์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ•œ๋ฐ”ํ€ด ๋Œ๋ฉด์„œ ์ž‘์€ ๊ฑธ ๋ฐœ๊ฒฌํ–ˆ์„๋•Œ, ๊ทธ ์ง€์ ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋ฐ€๊ณ  ๊ทธ ์ž๋ฆฌ์— ๋„ฃ๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด 25%์—์„œ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋‚˜์˜จ๋‹ค. ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ์•„๋‹ˆ๋ผ์„œ ๋ณต์žก๋„์— ๊ฑธ๋ฆฌ๋Š” ๊ฑด ์•„๋‹Œ ๊ฒƒ ๊ฐ™์€๋ฐ ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ์ง€ ์‚ดํŽด๋ณด์ž.


์ •๋‹ต์ฝ”๋“œ

import java.io.FileInputStream;
import java.util.Scanner;

class Main{
    public static void main(String[] args) throws Exception{
        System.setIn(new FileInputStream("res/input.txt"));

        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        int n = 20;
        for(int tc = 0; tc<T; tc++){
            int tcNum = sc.nextInt();
            int[] input = new int[n]; 
            int fallBack = 0;
            input[0] = sc.nextInt();
            for(int i=1; i<n;i++){
                input[i] = sc.nextInt();
                for(int j=i; j>0; j--){
                    if(input[j-1]>input[j]){
                        int tmpJ = input[j-1];
                        input[j-1] = input[j];
                        input[j] = tmpJ;
                        fallBack++;
                    }
                }
            }
            System.out.println(tcNum+" "+ fallBack);
        }
        sc.close();
    }
}

ํ›‘๋Š” ๋ฐฉ์‹์„ ๋ฐ”๊ฟ”๋ดค๋‹ค.

์ด์ „ ๋ฐฉ์‹์ด

  1. ์•ž์—์„œ ๋ถ€ํ„ฐ ํ˜„์žฌ ์‚ฝ์ž… ๊ฐ’๋ณด๋‹ค ํฐ ๊ฒƒ ์ฐพ๊ธฐ
  2. ์ฐพ์•˜์œผ๋ฉด ๊ทธ ์œ„์น˜๋ถ€ํ„ฐ ๋’ค๋กœ ์ญ‰ ํ•œ ์นธ ์”ฉ ๋ฐ€๊ธฐ
  3. ํ•ด๋‹น ์œ„์น˜์— ๊ฐ’ ์‚ฝ์ž…

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

์‹œ๊ฐ„ ๋ณต์žก๋„๋„ ์ค„๊ณ , ์™œ ๋‚ฌ๋Š” ์ง€ ๋ชจ๋ฅผ ์˜ค๋‹ต๋„ ๊ฑธ๋Ÿฌ๋‚ผ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

๋„์›€์ด ๋๋‹ค๋ฉด ๋Œ“๊ธ€์ด๋‚˜ ๊ณต๊ฐ ๋ฒ„ํŠผ ํ•œ ๋ฒˆ์”ฉ ๋ˆ„๋ฅด๊ณ  ๊ฐ€์ฃผ์„ธ์š”! ๋กœ๊ทธ์ธ ์•ˆํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค ^_^

 

๋ฐ˜์‘ํ˜•
COMMENT