3-2算法习题总结-二分

时间:2024-03-07 11:41:11

[COCI 2011/2012 #5] EKO / 砍树

题目描述

伐木工人 Mirko 需要砍 M M M 米长的木材。对 Mirko 来说这是很简单的工作,因为他有一个漂亮的新伐木机,可以如野火一般砍伐森林。不过,Mirko 只被允许砍伐一排树。

Mirko 的伐木机工作流程如下:Mirko 设置一个高度参数 H H H(米),伐木机升起一个巨大的锯片到高度 H H H,并锯掉所有树比 H H H 高的部分(当然,树木不高于 H H H 米的部分保持不变)。Mirko 就得到树木被锯下的部分。例如,如果一排树的高度分别为 20 , 15 , 10 20,15,10 20,15,10 17 17 17,Mirko 把锯片升到 15 15 15 米的高度,切割后树木剩下的高度将是 15 , 15 , 10 15,15,10 15,15,10 15 15 15,而 Mirko 将从第 1 1 1 棵树得到 5 5 5 米,从第 4 4 4 棵树得到 2 2 2 米,共得到 7 7 7 米木材。

Mirko 非常关注生态保护,所以他不会砍掉过多的木材。这也是他尽可能高地设定伐木机锯片的原因。请帮助 Mirko 找到伐木机锯片的最大的整数高度 H H H,使得他能得到的木材至少为 M M M 米。换句话说,如果再升高 1 1 1 米,他将得不到 M M M 米木材。

输入格式

1 1 1 2 2 2 个整数 N N N M M M N N N 表示树木的数量, M M M 表示需要的木材总长度。

2 2 2 N N N 个整数表示每棵树的高度。

输出格式

1 1 1 个整数,表示锯片的最高高度。

样例 #1

样例输入 #1

4 7
20 15 10 17

样例输出 #1

15

样例 #2

样例输入 #2

5 20
4 42 40 26 46

样例输出 #2

36

提示

对于 100 % 100\% 100% 的测试数据, 1 ≤ N ≤ 1 0 6 1\le N\le10^6 1N106 1 ≤ M ≤ 2 × 1 0 9 1\le M\le2\times10^9 1M2×109,树的高度 ≤ 4 × 1 0 5 \le 4\times 10^5 4×105,所有树的高度总和 > M >M >M

代码如下

package exercise.luogu.binary;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class P1873 {
    static long m;

    public static void main(String[] args) throws Exception {
        Read sc = new Read();
        int n = sc.nextInt();
        m = sc.nextInt();
        int[] trees = new int[n];
        for (int i = 0; i < trees.length; i++) {
            trees[i] = sc.nextInt();
        }

        int left = 0;
        int right = trees.length-1;

        while (left < right) {
            int mid = (left + right) / 2;
            if (testHeight(mid, trees)) {
                left = mid;
            } else {
                right = mid;
            }
        }
        System.out.println(trees[left]);
    }


    public static boolean testHeight(int standard, int[] arr) {
        int height = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] - arr[standard] > 0) {
                height += arr[i] - arr[standard];
            }
        }
        if (height < m) {
            return false;
        }
        return true;
    }

    static class Read {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        public int nextInt() throws Exception {
            st.nextToken();
            return (int) st.nval;
        }
    }

}

哪里出问题啊啊啊!!!
这是别人的代码!!!



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {
    public static void main(String[] args) throws Exception {
        Read r = new Read();
        int n = r.nextInt();
        long m = r.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = r.nextInt();
        }
        int left = -1,right = 400000;
        while (left+1<right){
            int mid = (left+right)/2;
            if(check(arr,mid,m)){
                left = mid;
            }else {
                right = mid;
            }
        }
        System.out.println(left);
    }
    public static boolean check(int[] arr,int mid,long m){
        long sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]-mid>0){
                sum+=arr[i]-mid;
            }
        }
        if(sum<m){
            return false;
        }
        return true;
    }

    static class Read{
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        public int nextInt() throws Exception{
            st.nextToken();
            return (int)st.nval;
        }
    }
}