๐Ÿ—๏ธ Algorithm/โน๏ธ SW Expert Academy [SWEA]

โน๏ธ[SW Expert Academy] [Python] [D3] [1208] Flatten

Dbswnstjd 2024. 3. 12. 23:06

๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&problemLevel=3&problemLevel=4&contestProbId=AV139KOaABgCFAYh&categoryId=AV139KOaABgCFAYh&categoryType=CODE&problemTitle=&orderBy=INQUERY_COUNT&selectCodeLang=ALL&select-1=4&pageSize=10&pageIndex=1

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

ํ’€์ด

์ž๊พธ ํ’€์ด๋Š” ํ•˜๊ณ  ์ด์ƒํ•œ ๊ณณ์—์„œ ์˜ค๋ฅ˜๋ฅผ ๋ฒ”ํ•œ๋‹ค. ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ด๋„ ํ‹€๋ฆฐ ๊ฒƒ์ด ์—†์—ˆ๋Š”๋ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ํ™•์ธํ•˜๋Š๋ผ T๋ฅผ 1๋กœ ์ง€์ •ํ•ด ๋†“์•˜๋‹ค. ์ฒ˜์Œ ํ‘ธ๋Š” ์‚ฌ์ดํŠธ๋‹ค ๋ณด๋‹ˆ ๋„ˆ๋ฌด ํ—ท๊ฐˆ๋ฆฌ๊ณ  ์“ธ๋ฐ์—†๋Š” ๊ณณ์— ์‹œ๊ฐ„์„ ๋‚ญ๋น„ํ•˜๊ฒŒ ๋œ๋‹ค..

 

D3 ๋ฌธ์ œ๋ฅผ ๋ช‡๊ฐœ ํ’€์–ด๋ณด๋‹ˆ ๋‚œ์ด๋„๊ฐ€ ํ•œ ๋ฐฑ์ค€ ์‹ค๋ฒ„3-4์ฏค ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋ง‰ ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์•„์ง ์•ˆํ’€์–ด๋ณด๊ธดํ–ˆ์ง€๋งŒ D4 ๊นŒ์ง€๋Š” ๋ฌด๋‚œํ•˜๊ฒŒ ๊ฐˆ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. 

T = 10
for test_case in range(1, T+1):
    dump = int(input())
    box = list(map(int, input().split()))
    
    for j in range(dump):
        high = max(box)
        low = min(box)

        high_index = box.index(max(box))
        low_index = box.index(min(box))
        
        box[high_index] -= 1
        box[low_index] += 1

    result = max(box) - min(box)
    print("#{} {}".format(test_case, result))

๊ฐ€์žฅ ๋†’์€ ๋ฐ•์Šค๋ฅผ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ณณ์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ€์žฅ ๋†’์€ ๋ฐ•์Šค์˜ ์ธ๋ฑ์Šค์™€ ๋†’์ด๋ฅผ ์ฐพ๊ณ  ๋‚ฎ์€ ๋ฐ•์Šค๋กœ ์˜ฎ๊ฒจ์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. ์ค‘๊ฐ„์— 0์ด ๋‚˜์˜ค๋”๋ผ๋„ ๊ฐ’์€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ break๋ฅผ ๊ฑธ์ง€ ์•Š์•˜๋‹ค.

 

Java ์ฝ”๋“œ

package SWEA;

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

public class swea_1208 {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        for(int t=1; t<=10; t++){
            int dump = Integer.parseInt(br.readLine());

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

            for(int i=0; i<dump; i++){
                Arrays.sort(arr);
                arr[0]++;
                arr[99]--;
            }
            Arrays.sort(arr);
            int result = arr[99] - arr[0];
            System.out.println("#" + t + " " + result);
        }
    }
}