Algorithm, Data structure/Solved Algorithmic Problem

BAEKJOON 2805 - EKO

JaykayChoi 2016. 12. 28. 23:09

문제: https://www.acmicpc.net/problem/2805


문제

Lumberjack Mirko needs to chop down M metres of wood. It is an easy job for him since he has a nifty new woodcutting machine that can take down forests like wildfire. However, Mirko is only allowed to cut a single row of trees.

Mirko‟s machine works as follows: Mirko sets a height parameter H (in metres), and the machine raises a giant sawblade to that height and cuts off all tree parts higher than H (of course, trees not higher than H meters remain intact). Mirko then takes the parts that were cut off. For example, if the tree row contains trees with heights of 20, 15, 10, and 17 metres, and Mirko raises his sawblade to 15 metres, the remaining tree heights after cutting will be 15, 15, 10, and 15 metres, respectively, while Mirko will take 5 metres off the first tree and 2 metres off the fourth tree (7 metres of wood in total).

Mirko is ecologically minded, so he doesn‟t want to cut off more wood than necessary. That‟s why he wants to set his sawblade as high as possible. Help Mirko find the maximum integer height of the sawblade that still allows him to cut off at least M metres of wood.

입력

The first line of input contains two space-separated positive integers, N (the number of trees, 1 ≤ N ≤ 1 000 000) and M (Mirko‟s required wood amount, 1 ≤ M ≤ 2 000 000 000).

The second line of input contains N space-separated positive integers less than 1 000 000 000, the heights of each tree (in metres). The sum of all heights will exceed M, thus Mirko will always be able to obtain the required amount of wood.

출력

The first and only line of output must contain the required height setting.

 

예제 입력 

4 7
20 15 10 17

예제 출력 

15

힌트



역시 이진탐색을 이용하는 문제입니다. 기존 이진탐색 코드에서 주의해서 변경할 점은 target 높이가 높을 때 더 작은 값을 얻을 수 있다는 점입니다.

그리고 또 하나 주의할 점은 잘려진 나무의 길이를 구할 때 오버플로를 대비해서 long long 을 사용해야 된다는 점입니다. 



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
#pragma warning (disable:4996)
#include <stdio.h>
#include <cstring>
#include <climits>
#include <algorithm>
 
using namespace std;
typedef long long ll;
 
ll getTotalLength(const int* numbers, int height, int n)
{
    ll ret = 0;
    for (int i = 0; i < n; i++)
        ret += max(0, numbers[i] - height);
    return ret;
}
 
int binarySearch(const int* numbers, int low, int high, ll target, int n)
{
    while (low <= high)
    {
        int mid = (low + high) / 2;
        ll totalLength = getTotalLength(numbers, mid, n);
        if (totalLength == target)
            return mid;
        else if (totalLength > target)
            low = mid + 1;
        else if (target > totalLength)
            high = mid - 1;
    }
 
    return high;
}
 
int main()
{
    int n, m, lowest, hightest;
    lowest = 0;
    hightest = INT_MIN;
    scanf("%d"&n);
    scanf("%d"&m);
    int* trees = new int[n];
    for (int i = 0; i < n; i++)
    {
        scanf("%d"&trees[i]);
        hightest = max(hightest, trees[i]);
    }
 
    printf("%d\n", binarySearch(trees, lowest, hightest, m, n));
    delete[] trees;
    return 0;
}
cs


'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글

BAEKJOON 1654 - 랜선 자르기  (0) 2016.12.30
BAEKJOON 2512 - 예산  (0) 2016.12.30
BAEKJOON 2869 - PUŽ  (0) 2016.12.26
BAEKJOON 10815 - 숫자 카드  (0) 2016.12.26
BAEKJOON 1920 - 수 찾기  (0) 2016.12.25