less than 1 minute read

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

๐Ÿฅˆ Silver 4


๐Ÿ“Œ ๋ฌธ์ œ

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


image

image


๐Ÿ“Œ ํ’€์ด

์ด ๋ฌธ์ œ๋ฅผ ์ฒ˜์Œ ๋ณด์ž๋งˆ์ž ๋“  ์ƒ๊ฐ์ด ์žˆ์—ˆ๋‹ค.
๋ฐ”๋กœ ์ผ๋‹จ ์ •๋ ฌ์„ ํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด์—ˆ๋‹ค.

์ผ๋‹จ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ›์€ ๊ฐ’๋“ค์„ ๋ฐฐ์—ด์— ๋„ฃ์–ด์ฃผ๊ณ , Arrays.sort ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ›์€ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•ด์ฃผ์—ˆ๋‹ค.
์ดํ›„, ์ด์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ people๊นŒ์ง€ i๋ฅผ ๋Œ๋ฆฌ๊ณ , j๋ฅผ i+1๋งŒํผ ํ•œ๋ฒˆ ๋” ๋Œ๋ฆฌ๋ฉด์„œ ์ด์ „๊นŒ์ง€์˜ ๋ฐฐ์—ด์— ์žˆ๋Š” ๊ฐ’์„ result์— ๋”ํ•ด์ฃผ์—ˆ๋‹ค.

Greedy ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ƒ๊ฐ๋ณด๋‹ค ์‰ฝ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.


๐Ÿ“Œ Code

package Greedy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class N11399 {
    public static int people;
    public static int[] times;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        people = Integer.parseInt(st.nextToken());
        times = new int[people];

        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < people; i++) {
            times[i] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(times);
        int result = 0;
        for (int i = 0; i < people; i++) {
            for (int j = 0; j < i +1; j++) {
                result += times[j];
            }
        }

        System.out.println(result);
    }
}

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



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