๊ธฐ๋ก๋ฐฉ

BOJ_25945 : ์ปจํ…Œ์ด๋„ˆ ์žฌ๋ฐฐ์น˜ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_25945 : ์ปจํ…Œ์ด๋„ˆ ์žฌ๋ฐฐ์น˜

Soom_1n 2024. 6. 15. 15:15

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



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

  • ์ด m๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ n๊ฐœ์˜ ์นธ์— ๋‚˜๋ˆ„์–ด ์Œ“์—ฌ์ ธ ์žˆ๋‹ค.
  • n๊ฐœ์˜ ๊ฐ ์นธ์˜ ์ปจํ…Œ์ด๋„ˆ ๋†’์ด๊ฐ€ 1์ดํ•˜๊ฐ€ ๋˜๋„๋ก ์žฌ๋ฐฐ์น˜ํ•  ๋•Œ, ์ตœ์†Œ ์ด๋™ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

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

  • ์ปจํ…Œ์ด๋„ˆ์˜ ๋†’์ด๋ฅผ ํ‰ํƒ„ํ™” ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
  • ๊ฐ€์žฅ ๋จผ์ € ์ •๋ ฌ์„ n๋ฒˆ ์ˆ˜ํ–‰ํ•ด์„œ ์ตœ๋Œ€๊ฐ’์„  -1 ์ตœ์†Œ๊ฐ’์„ +1 ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค
    • n์ด ์ตœ๋Œ€ 10๋งŒ์ด๋ฏ€๋กœ, ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n^2logn) ์œผ๋กœ 1์ดˆ๋ฅผ ์ดˆ๊ณผํ•˜๊ฒŒ ๋œ๋‹ค.
    • ์ตœ๋Œ€ํž™, ์ตœ์†Œํž™ ๋‘ ๊ฐ€์ง€ ์šฐ์„ ์ˆœ์œ„ ํ๋กœ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’๋งŒ ๋น ๋ฅด๊ฒŒ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ์ด ๋ฌธ์ œ์—์„œ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ฒŒ ๋œ๋‹ค.
  • ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํšจ์œจ์ ์œผ๋กœ ๊ณ„์‚ฐํ•ด์•ผํ•œ๋‹ค.
    • n๊ฐœ์˜ ๊ฐ ์นธ์— ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ฒฐ๊ตญ ํ‰๊ท (m/n)์— ์ˆ˜๋ ดํ•˜๊ฒŒ ๋œ๋‹ค.
    • ์—ฌ๊ธฐ์„œ ๋†’์ด์ฐจ๊ฐ€ 1์ดํ•˜์ธ ์ด์œ ๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ, ํ‰๊ท ์— ์ˆ˜๋ ดํ•  ๋•Œ ๋ช‡ ๊ฐœ์˜ ์นธ์€ ํ‰๊ท +1์˜ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค.(m%n ๊ฐœ)
    • ์—ฌ๊ธฐ์„œ ์ •๋ ฌ์ด ํ•„์š”ํ•œ๋ฐ, ํ‰๊ท ๋ณด๋‹ค ์ž‘์€ ๋†’์ด์˜ ์นธ์—์„œ ํ‰๊ท +1 ๊นŒ์ง€ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹Œ
      ํ‰๊ท  ๋ณด๋‹ค ํฐ ๋†’์ด์˜ ์นธ์—์„œ ํ‰๊ท +1 ๊นŒ์ง€ ๋‚ด๋ ค์•ผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ •๋ ฌ์ด ํ•„์š”ํ•˜๋‹ค.

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

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

public class Main {
    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());
        Integer[] containers = new Integer[n];
        long sum = 0;

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

        Arrays.sort(containers, Collections.reverseOrder());

        int avg = (int) (sum / n);
        int remainder = (int) (sum % n);

        int[] target = new int[n];
        for (int i = 0; i < n; i++) {
            target[i] = avg + (i < remainder ? 1 : 0);
        }

        long moveCnt = 0;
        for (int i = 0; i < n; i++) {
            if (containers[i] > target[i]) {
                moveCnt += containers[i] - target[i];
            }
        }

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

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

  • n๊ฐœ์˜ ์นธ์˜ ์ปจํ…Œ์ด๋„ˆ ์ •๋ณด๋ฅผ Integer ๋ฐฐ์—ด containers์— ์ €์žฅํ•˜๊ณ , ์ดํ•ฉ์„ longํ˜• ๋ณ€์ˆ˜ sum์— ๋ˆ„์ ํ•œ๋‹ค.
  • containers๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
  • ์ˆ˜๋ ดํ•  ํ‰๊ท  avg๋Š” sum/n, ํ‰๊ท +1์˜ ๊ฐœ์ˆ˜์ธ remainder๋Š” sum%n์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค.
  • containers ๋ฐฐ์—ด๊ณผ ๋งค์นญ ์‹œํ‚ฌ int๋ฐฐ์—ด target์— remainder๋งŒํผ avg+1, ๋‚˜๋จธ์ง€๋Š” agv ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.
  • n๊ฐœ์˜ ์นธ์„ ํ™•์ธํ•ด์„œ target์˜ ๊ฐ’์ด ๋” ํฐ ๊ฒฝ์šฐ, ๊ทธ ์ฐจ์ด๋งŒํผ moveCnt์— ๋ˆ„์ ํ•œ๋‹ค.
    • target๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ๋Š” ์ค‘๋ณต์ด๋ฏ€๋กœ ์นด์šดํŠธํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • moveCnt๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ฒ˜์Œ์— 2๊ฐœ์˜ ์šฐ์„ ์ˆœ์œ„ ํ๋กœ ์ตœ๋Œ€ํž™, ์ตœ์†Œํž™์„ ํ†ตํ•ด ์ตœ๋Œ€๊ฐ’, ์ตœ์†Œ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์ดํ–ˆ๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค.
  • ์‹œ๊ฐ„ ๋ณต์žก๋„ ์ƒ์œผ๋กœ๋Š” O(nlogn)์œผ๋กœ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™์•˜๋Š”๋ฐ, ์•„๋งˆ ๊ทธ๋ฆฌ๋”” ํ’€์ด๋ฅผ ์˜๋„ํ•œ ๋ฌธ์ œ์—ฌ์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚œ ๊ฒƒ ๊ฐ™๋‹ค.
  • ์ž์ฃผ ๋“ฑ์žฅํ•  ๊ฒƒ ๊ฐ™์€ ์œ ์šฉํ•œ ๊ทธ๋ฆฌ๋”” ์œ ํ˜•์ธ ๊ฒƒ ๊ฐ™์•„์„œ ์ž˜ ๊ธฐ์–ตํ•ด ๋‘˜ ์ƒ๊ฐ์ด๋‹ค.
728x90