蓝桥 卡牌 二分 long

🍑 算法题解专栏


🍑 蓝桥国赛 卡牌

在这里插入图片描述
输入

4 5
1 2 3 4
5 5 5 5

输出

3

🍑 m <= n^2 --> m <= 4*10^10:开 long存 m
🍑 二分答案(注意答案范围)

import java.util.*;

public class Main
{
	static int n, N = 200010;
	static long m;
	static int[] a = new int[N];
	static int[] b = new int[N];

	static boolean check(int x)
	{
		long t = m;// 临时变量记录空白牌数量
		for (int i = 0; i < n; i++)
		{
			if (a[i] >= x)// 已有的 卡牌i 的数量 有 x 张
				continue;
			// 已有+可凑的卡牌i >= x 并且 空白卡牌数量 >= 需要凑的卡牌数量
			else if (a[i] + b[i] >= x && t >= x - a[i])
			{
				t = t - x + a[i];// 减去凑的卡牌数量
				continue;
			} else
			{
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextLong();// m 最大是 n^2,开 long long
		int mx = 0;
		for (int i = 0; i < n; i++)
		{
			a[i] = sc.nextInt();
			mx = Math.max(mx, a[i]);
		}

		for (int i = 0; i < n; i++)
			b[i] = sc.nextInt();

		int l = 0;
		int r = Integer.MAX_VALUE;// 其实 a[i]+b[i] 最大为 400000,r 为
		while (l < r)
		{
			int mid = l + r + 1 >> 1;
			if (check(mid))
				l = mid;
			else
				r = mid - 1;
		}
		System.out.println(r);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值