卡牌 二分

文章描述了一个Java程序,使用二分查找算法解决了一个关于分配卡片的问题,确保每张卡片能满足条件且不超过给定限制。程序读取输入,处理数组并检查是否满足条件,输出最小的符合条件的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
	static int[]a;
	static int[]b;
	static int n = 0;
	static long m = 0;
    public static void main(String[] args) throws Exception{
    	BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[]s = reader.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        m = Long.parseLong(s[1]);
        a = new int[n];
        b = new int[n];
        String[]s1 = reader.readLine().split(" ");
        for (int i = 0; i < s1.length; i++) {
			a[i] = Integer.parseInt(s1[i]);
		}
        String[]s2 = reader.readLine().split(" ");
        for (int i = 0; i < s2.length; i++) {
			b[i] = Integer.parseInt(s2[i]);
		}
        int l = 0;
        int r = 2*n;
        while (l<r) {
			int mid = l+r+1>>1;
            if (check(mid)) {
				l = mid;
			}else {
				r = mid-1;
			}
		}
        System.out.println(l);
    }
    //1 2 3 4 5
    private static boolean check(int mid) {
    	long temp = m;
    	for (int i = 0; i < n; i++) {
			if (a[i]>=mid) {
				continue;
			}
			if (mid-a[i]>temp) {//空白牌不够
				return false;
			}
			if (mid-a[i]>b[i]) {//超过b[i]
				return false;
			}
			if (mid-a[i]<=temp) {
				temp-=(mid-a[i]);
			}else {
				return false;
			}
		}
		return true;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值