Solution

Memo

  • 파라메트릭서치
  • 이건 처음 보는 것 같다.
  • 이분탐색의 응용
  • t/f 평가값중 마지막 t의 평가값을 찾는다.
  • 상황 자체를 일종의 정렬된 배열로 보면 편하다고 한다.
  • https://gliver.tistory.com/31
// Baekjoon - 2805
// https://www.acmicpc.net/problem/2805

use std::io::{self, Read, Write};

fn main() {
    let mut stdin = io::stdin().lock();
    let mut stdout = io::stdout().lock();
    let mut input = String::new();
    stdin.read_to_string(&mut input).unwrap();
    let mut lines = input.lines();

    let first_line = lines.next().unwrap();
    let parts: Vec<u64> = first_line
        .split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();
    let _n = parts[0];
    let need = parts[1];

    let trees: Vec<u64> = lines.next().unwrap()
        .split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();

    let mut left = 0;
    let mut right = *trees.iter().max().unwrap();
    let mut result = 0;

    while left <= right {
        let mid = (left + right) / 2;

        let amount: u64 = trees.iter()
            .map(|&height| if height > mid { height - mid } else { 0 })
            .sum();

        if amount >= need {
            result = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }

    write!(stdout, "{}", result).unwrap();
}