๊ธฐ๋ก๋ฐฉ

BOJ_2343 : ๊ธฐํƒ€ ๋ ˆ์Šจ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_2343 : ๊ธฐํƒ€ ๋ ˆ์Šจ

Soom_1n 2024. 2. 5. 13:19

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

 

2343๋ฒˆ: ๊ธฐํƒ€ ๋ ˆ์Šจ

๊ฐ•ํ† ๋Š” ์ž์‹ ์˜ ๊ธฐํƒ€ ๊ฐ•์˜ ๋™์˜์ƒ์„ ๋ธ”๋ฃจ๋ ˆ์ด๋กœ ๋งŒ๋“ค์–ด ํŒ๋งคํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋ธ”๋ฃจ๋ ˆ์ด์—๋Š” ์ด N๊ฐœ์˜ ๊ฐ•์˜๊ฐ€ ๋“ค์–ด๊ฐ€๋Š”๋ฐ, ๋ธ”๋ฃจ๋ ˆ์ด๋ฅผ ๋…นํ™”ํ•  ๋•Œ, ๊ฐ•์˜์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ์•ˆ ๋œ๋‹ค. ์ˆœ์„œ๊ฐ€ ๋’ค๋ฐ”๋€Œ๋Š” ๊ฒฝ

www.acmicpc.net



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

  • N๊ฐœ์˜ ์˜์ƒ์„ M๊ฐœ์˜ ๋ธ”๋ฃจ๋ ˆ์ด์— ๋‚˜๋ˆ  ์ €์žฅํ•˜๋ ค๊ณ  ํ•œ๋‹ค.
  • ํ•˜๋‚˜์˜ ๋ธ”๋ฃจ๋ ˆ์ด ํฌ๊ธฐ์˜ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

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

  • ๋ธ”๋ฃจ๋ ˆ์ด ํฌ๊ธฐ๋ฅผ ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ ์ฐพ๋Š”๋‹ค.
    • ์ตœ์†Œ ํฌ๊ธฐ๋Š” N๊ฐœ์˜ ์˜์ƒ ์ค‘ ์ตœ๋Œ€๊ฐ’
    • ์ตœ๋Œ€ ํฌ๊ธฐ๋Š” N๊ฐœ์˜ ์˜์ƒ์˜ ์ดํ•ฉ
    • ์ค‘์•™๊ฐ’์„ ์‹œ์ž‘์œผ๋กœ M๊ฐœ์˜ ๋ธ”๋ฃจ๋ ˆ์ด์— ๋ชจ๋‘ ์ €์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ํฌ๊ธฐ๋ฅผ ์ค„์—ฌ๋ณด๊ณ ,
      ๋ชจ๋‘ ์ €์žฅํ•  ์ˆ˜ ์—†์œผ๋ฉด ์ €์žฅ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ ค๋ณด๋Š” ์‹์œผ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.

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

import java.io.*;
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));
        StringTokenizer st = new StringTokenizer(br.readLine());
        StringBuilder sb = new StringBuilder();

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        int[] arr = new int[N];
        st = new StringTokenizer(br.readLine());

        int start = 0; // ์‹œ์ž‘ ์ธ๋ฑ์Šค :
        int end = 0;

        for (int i = 0; i < N; i++) {
            arr[i] = Integer.parseInt(st.nextToken());
            start = Math.max(start, arr[i]);
            end += arr[i];
        }

        while (start <= end) {
            int mid = (start + end) / 2;
            int sum = 0;
            int cnt = 0;
            for (int i = 0; i < N; i++) {
                if (sum + arr[i] > mid) {
                    sum = 0;
                    cnt++;
                }
                sum += arr[i];
            }
            if (sum > 0) cnt++;
            if (cnt > M) start = mid + 1;
            else end = mid - 1;
        }

        bw.write(Integer.toString(start));
        bw.flush();
    }
}

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

  • ์ด์ง„ํƒ์ƒ‰์œผ๋กœ ๋ธ”๋ฃจ๋ ˆ์ด ํฌ๊ธฐ์˜ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
    • ์‹œ์ž‘ ์ธ๋ฑ์Šค๋Š” ์˜์ƒ ์ค‘ ์ตœ๋Œ€๊ฐ’(๋ธ”๋ฃจ๋ ˆ์ด๊ฐ€ ์ด ๋ณด๋‹ค ์ž‘์œผ๋ฉด ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค)
    • ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋Š” ์˜์ƒ์˜ ์ดํ•ฉ(๋ธ”๋ฃจ๋ ˆ์ด๊ฐ€ ๋” ์ปค์งˆ ํ•„์š”๊ฐ€ ์—†๋‹ค)
  • ์ค‘์•™๊ฐ’ mid๋Š” ํ˜„์žฌ ๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ์ด๋‹ค.
  • sum์€ ํ˜„์žฌ ๋ธ”๋ฃจ๋ ˆ์ด์— ์ €์žฅํ•œ ์˜์ƒ ๊ธธ์ด์˜ ํ•ฉ, cnt๋Š” ํ˜„์žฌ๊นŒ์ง€ ์‚ฌ์šฉํ•œ ๋ธ”๋ฃจ๋ ˆ์ด์˜ ๊ฐœ์ˆ˜์ด๋‹ค.
    • ์ €์žฅํ•œ ์˜์ƒ์ด mid๋ณด๋‹ค ๊ธธ์–ด์ง€๋ฉด cnt๋ฅผ + 1ํ•œ๋‹ค.
  • ๋ชจ๋“  ์˜์ƒ์„ ํ™•์ธํ•œ ํ›„ ๋‹ค์Œ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•œ๋‹ค.
    • cnt > M ์ด๋ฉด, ๋ธ”๋ฃจ๋ ˆ์ด ๊ณต๊ฐ„์ด ๋ถ€์กฑํ–ˆ๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ start๋ฅผ mid+1๋กœ ๋ฐ”๊พผ๋‹ค.
    • cnt <= M ์ด๋ฉด, ๋ธ”๋ฃจ๋ ˆ์ด ๊ณต๊ฐ„์ด ์ ํ•ฉํ•˜๊ฑฐ๋‚˜ ๋„˜์ณค๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ end๋ฅผ mid-1๋กœ ๋ฐ”๊พผ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ด์ง„ ํƒ์ƒ‰์„ ๋‹จ์ˆœ ๋ฐฐ์—ด ํƒ์ƒ‰์— ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ’์— ์ ์šฉํ•œ ๋ฌธ์ œ์ด๋‹ค.
    • ์ด๋ ‡๊ฒŒ ์“ฐ์ธ๋‹ค๋Š” ๊ฒƒ์€ ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ์•„์ง ์ต์ˆ™ํ•ด์ง€์ง€ ์•Š์•˜๋‹ค.
    • ์ด๋Ÿฐ ๋ฐฉ์‹์˜ ๋ฌธ์ œ๋ฅผ ๋” ํ’€์–ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.
    • ์ด๋Ÿฐ ๋ฐฉ์‹์˜ ์ด์ง„ ํƒ์ƒ‰์„ ๋งค๊ฐœ ๋ณ€์ˆ˜ ํƒ์ƒ‰์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_1715 : ์นด๋“œ ์ •๋ ฌํ•˜๊ธฐ  (0) 2024.02.11
BOJ_1300 : K๋ฒˆ์งธ ์ˆ˜  (0) 2024.02.05
BOJ_11404 : ํ”Œ๋กœ์ด๋“œ  (0) 2024.01.31
BOJ_1517 : ๋ฒ„๋ธ” ์†ŒํŠธ  (0) 2024.01.26
BOJ_2638 : ์น˜์ฆˆ  (0) 2024.01.25