3-2算法习题总结

贪心问题

[USACO1.3] 混合牛奶 Mixing Milk

题目描述

由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要。帮助 Marry 乳业找到最优的牛奶采购方案。

Marry 乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格可能相同。此外,就像每头奶牛每天只能挤出固定数量的奶,每位奶农每天能提供的牛奶数量是一定的。每天 Marry 乳业可以从奶农手中采购到小于或者等于奶农最大产量的整数数量的牛奶。

给出 Marry 乳业每天对牛奶的需求量,还有每位奶农提供的牛奶单价和产量。计算采购足够数量的牛奶所需的最小花费。

注:每天所有奶农的总产量大于 Marry 乳业的需求量。

输入格式

第一行二个整数 n , m n,m n,m,表示需要牛奶的总量,和提供牛奶的农民个数。

接下来 m m m 行,每行两个整数 p i , a i p_i,a_i pi,ai,表示第 i i i 个农民牛奶的单价,和农民 i i i 一天最多能卖出的牛奶量。

输出格式

单独的一行包含单独的一个整数,表示 Marry 的牛奶制造公司拿到所需的牛奶所要的最小费用。

样例 #1

样例输入 #1

100 5
5 20
9 40
3 10
8 80
6 30

样例输出 #1

630

提示

【数据范围】
对于 100 % 100\% 100% 的数据:
0 ≤ n , a i ≤ 2 × 1 0 6 0 \le n,a_i \le 2 \times 10^6 0n,ai2×106 0 ≤ m ≤ 5000 0\le m \le 5000 0m5000 0 ≤ p i ≤ 1000 0 \le p_i \le 1000 0pi1000

题目翻译来自 NOCOW。

USACO Training Section 1.3

代码如下

package exercise.luogu.greedy;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class P1208 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] product = new int[m][2];
        for (int i = 0; i < m; i++) {
            product[i][0] = sc.nextInt();
            product[i][1] = sc.nextInt();
        }
        Arrays.sort(product, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[0] - o2[0];
            }
        });
        int productCount = 0;
        int money = 0;

        for (int i = 0; i < product.length; i++) {
            if (productCount < n) {
                if (product[i][1] <= (n - productCount)) {
                    money += product[i][0] * product[i][1];
                    productCount+=product[i][1];
                } else {
                    money += product[i][0] * (n - productCount);
                    productCount=n;
                }
            }
        }
        System.out.println(money);
    }
}

二分

[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;
        }
    }
}
  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知意..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值