Game Develop

[Algorithm]Baekjoon 17951번 :: 흩날리는 시험지 속에서 내 평점이 느껴진거야 본문

Algorithm/Algorithm

[Algorithm]Baekjoon 17951번 :: 흩날리는 시험지 속에서 내 평점이 느껴진거야

MaxLevel 2025. 9. 18. 02:58

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>
#include <functional>
#include <sstream>
#include <memory.h>
#include <deque>
#include <set>
#include <unordered_map>
#include <stack>
#include <numeric>
#include <climits>
#include <bitset>
#include <cmath>
#include <mutex>
 
using namespace std;
 
int n, k;
int papers[100000];
 
 
bool check(int mid)
{
    int sum = 0;
    int count = k;
    
    for (int i = 0; i < n; ++i)
    {
        sum += papers[i];
 
        if (sum >= mid)
        {
            --count; // 그룹개수 카운팅
            sum = 0;
 
            if (count == 0)
            {
                return true;
            }
        }
    }
 
    return false;
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
 
    cin >> n >> k;
 
    for (int i = 0; i < n; ++i)
    {
        cin >> papers[i];
    }
 
    int left = 0;
    int right = 0x3f3f3f3f;
    int answer = 0;
 
    while (left <= right)
    {
        int mid = (left + right) / 2;
 
        if (check(mid))
        {
            left = mid + 1;
            answer = mid;
        }
        else
        {
            right = mid - 1;
        }
    }
 
    cout << answer;
}
cs

 

 

주어진 문자 그대로 완탐을 한다고 가정하면, 그룹을 가지각색으로 나눠야 하기 때문에 시간복잡도가 매우매우 복잡해진다.

그러니 구해야하는 최소값을 고정시킨 후, 시험지를 앞에서부터 검사하면서 최소값을 만족시키는 누적값이 완성될 때마다 카운팅을 한다.

이 카운팅값이 k값보다 크다면, 즉 조건을 만족할 경우 이 고정된 최소값은 문제에서 요구하는 값들 중 하나가 된다.

이렇게 이 고정값들을 이분탐색시키면서 조절하면 정해를 구할 수 있다.

'Algorithm > Algorithm' 카테고리의 다른 글

[Algorithm]Baekjoon 1007번 :: 벡터 매칭  (0) 2025.11.04
[Algorithm]Baekjoon 1461번 :: 도서관  (0) 2025.09.19
[Algorithm]Baekjoon 3079번 :: 입국심사  (0) 2025.09.17
LCS  (0) 2023.11.06
MergeSort와 QuickSort  (0) 2022.12.27