๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][BOJ] 2343 ๊ธฐํƒ€ ๋ ˆ์Šจ (feat. ์ด๋ถ„ํƒ์ƒ‰)

ibelieveinme 2024. 5. 23. 01:17
728x90

 

[๋ฌธ์ œ]

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

 

N ๊ฐœ์˜ ๊ฐ•์˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ•์˜๋“ค์„ ๋ช‡๊ฐœ์”ฉ ์—ฎ์–ด์„œ ๋ธ”๋ฃจ๋ ˆ์ด๋กœ ๋งŒ๋“ค์–ด์„œ ํŒ๋งคํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

๊ฐ•์˜๋“ค์„ ์—ฎ์„ ๋•Œ ๊ฐ•์˜์˜ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด์•ˆ๋˜๊ณ , ๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ๋Š” ๋ชจ๋‘ ๊ฐ™์•„์•ผ ํ•œ๋‹ค.

์ฆ‰, ๊ฐ•์˜๋ฅผ ์ชผ๊ฐค ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œํ•ฉ์„ ์ฐพ์ž.

 

*๋ฌธ์ œ์กฐ๊ฑด

๊ฐ•์˜์˜ ์ˆ˜  N (1 ≤ N ≤ 100,000)๊ณผ M (1 ≤ M ≤ N)

๊ฐ ๊ฐ•์˜์˜ ๊ธธ์ด๋Š” 10,000๋ถ„์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

 

[์˜ˆ์‹œ]

9 3
1 2 3 4 5 6 7 8 9
17

 

๊ฐ•์˜๋Š” ์ด 9๊ฐœ์ด๊ณ , ๋ธ”๋ฃจ๋ ˆ์ด๋Š” ์ด 3๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

 

1๋ฒˆ ๋ธ”๋ฃจ๋ ˆ์ด์— 1, 2, 3, 4, 5

2๋ฒˆ ๋ธ”๋ฃจ๋ ˆ์ด์— 6, 7

3๋ฒˆ ๋ธ”๋ฃจ๋ ˆ์ด์— 8, 9

 

์„ ๋„ฃ์œผ๋ฉด ๊ฐ ๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ๋Š” 15, 13, 17์ด ๋œ๋‹ค.

 

๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ๋Š” ๋ชจ๋‘ ๊ฐ™์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ธ”๋ฃจ๋ ˆ์ด์˜ ํฌ๊ธฐ๋Š” 17์ด ๋œ๋‹ค.

17๋ณด๋‹ค ๋” ์ž‘์€ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋Š” ๋ธ”๋ฃจ๋ ˆ์ด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†๋‹ค.

 

[ํ’€์ด]

๊ฐ•์˜์˜ ์ˆ˜๊ฐ€ N (1 ≤ N ≤ 100,000)์ด๊ณ  ๋ธ”๋ฃจ๋ ˆ์ด์˜ ์ˆ˜ M (1 ≤ M ≤ N) ๊ฐ’์„ ๊ฐ€์ง€๋ฏ€๋กœ

๋‹จ์ˆœํ•˜๊ฒŒ ํ’€๋ฉด  NC1 + NC2 + NC3 + ... + NCN = 2^N -1๋กœ ๋งค์šฐ ํฐ ์ˆ˜๊ฐ€ ๋˜์–ด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ํƒ์ƒ‰ ์ˆ˜๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์ด๋ถ„ํƒ์ƒ‰์„ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

์ด๋ถ„ํƒ์ƒ‰์€ ์ •๋ ฌ ํ›„, ํƒ์ƒ‰์˜ ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์„ ์ •ํ•ด๋‘์–ด์•ผ ํ•œ๋‹ค.

๋ธ”๋ฃจ๋ ˆ์ด ์ตœ์†Œ๊ฐ’ : ๊ฐ€์žฅ ๊ธด ๊ฐ•์˜์˜ ๊ธธ์ด

๋ธ”๋ฃจ๋ ˆ์ด ์ตœ๋Œ€๊ฐ’ : ๋ชจ๋“  ๊ฐ•์˜ ๊ธธ์ด์˜ ํ•ฉ

 

๋ธ”๋ฃจ๋ ˆ์ด๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์˜ ์ค‘๊ฐ„๊ฐ’์„ ์ •๋‹ต์œผ๋กœ ๋ฏธ๋ฆฌ ์ง€์ •ํ•ด๋‘๊ณ  ๊ฐ•์˜๊ฐ€ ๋‚˜๋ˆ ์ง€๋Š”์ง€ ํŒ๋‹จํ•œ๋‹ค.

 

์ฆ‰,

์ค‘๊ฐ„๊ฐ’ = (์ตœ์†Œ๊ฐ’ + ์ตœ๋Œ€๊ฐ’) / 2

 

1) 1๋ฒˆ ๊ฐ•์˜๋ถ€ํ„ฐ ๊ฐ•์˜๋“ค์˜ ํ•ฉ์˜ ์ค‘๊ฐ„ ๊ฐ’๋ณด๋‹ค ์ปค์งˆ ๋•Œ๊นŒ์ง€ ๊ฐ•์˜์˜ ์ˆ˜๋ฅผ ๋”ํ•œ๋‹ค.

2) ์ค‘๊ฐ„๊ฐ’์„ ๋„˜์–ด๊ฐ€๋ฉด 1์„ ์นด์šดํŠธํ•˜๊ณ  ๊ฐ•์˜ ํ•ฉ์„ 0์œผ๋กœ ๋ฆฌ์…‹ํ•˜๊ณ  ๊ฐ•์˜๋ฅผ ๋‹ค์‹œ ๋”ํ•œ๋‹ค.

3) ๋งˆ์ง€๋ง‰ ๊ฐ•์˜๊นŒ์ง€ ํ•ฉ์„ ๊ตฌํ–ˆ์œผ๋ฉด ์นด์šดํŠธ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์ตœ์†Œ๊ฐ’, ์ตœ๋Œ€๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค.

    3-1) ์นด์šดํŠธ ๊ฐ’ < ๋ธ”๋ฃจ๋ ˆ์ด ๊ฐœ์ˆ˜ ์ด๋ฉด, ๋” ์ž‘์€ ํฌ๊ธฐ ๊ฐ’์œผ๋กœ ์ชผ๊ฐœ์•ผ ํ•˜๋ฏ€๋กœ ์ตœ๋Œ€๊ฐ’ = ์ค‘๊ฐ„๊ฐ’ -1 ๋กœ ๋‘๊ณ  ๋‹ค์‹œ ํƒ์ƒ‰ํ•œ๋‹ค.

    3-2) ์นด์šดํŠธ ๊ฐ’ > ๋ธ”๋ฃจ๋ ˆ์ด ๊ฐœ์ˆ˜ ์ด๋ฉด, ๋” ํฐ ํฌ๊ธฐ ๊ฐ’์œผ๋กœ ์ชผ๊ฐœ์•ผ ํ•˜๋ฏ€๋กœ ์ตœ์†Œ๊ฐ’ = ์ค‘๊ฐ„๊ฐ’ + 1 ๋กœ ๋‘๊ณ  ๋‹ค์‹œ ํƒ์ƒ‰ํ•œ๋‹ค.

 

 

[์ฝ”๋“œ]

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int lectureNum, bluerayNum, input;
    int l, r = 0, mid = 0; // left, right, mid
    vector<int> lectures;

    // 1. Input
    cin >> lectureNum >> bluerayNum;
    for (int i = 0; i < lectureNum; i++) {
        cin >> input;
        lectures.push_back(input);
        r += input;
    }

    // 2. SearchByBinary
    l = *max_element(lectures.begin(), lectures.end());

    while (l <= r) {
        mid = (l + r) / 2;

        int sum = 0, count = 0;
        for (int i = 0; i < lectureNum; i++) {
            if (sum + lectures[i] > mid) {
                count++;
                sum = 0;
            }
            sum += lectures[i];
        }
        if (sum != 0) count++;

        if (count > bluerayNum) l = mid + 1;
        else  r = mid - 1;
    }

    // 3. Output
    cout << l << "\n";

    return 0;
}

 

 

[๋ฐ˜๋ก€]

7 7
5 9 6 8 7 7 5

answer: 9

 

4 3
99 1 99 1

answer: 100

 

 

728x90