codeforce之Magic Powder

本文介绍了一个关于烘焙饼干的算法问题,通过使用二分查找法来确定最大可烘焙饼干数量。问题考虑了原料限制及魔法粉末的辅助作用,采用C++实现。

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

题目:

The term of this problem is the same as the previous one, the only exception — increased restrictions.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.


解答:

首先很很明显这是一个对结果进行二分的题目,

注意要用long long int,同时在判断目标答案是否可行的时候用减法不要用加法,否则可能会溢出

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#define ll long long int
using namespace std;

struct vege {
	ll id;
	ll per;
	ll total;
	ll alre;
	vege() {
	}
};

ll cost(vector<vege> &ingr, int tar,ll k) {
	ll res = 0;
	ll size = ingr.size();
	for (int i = 0; i < size; ++i) {
		if (k < 0)
			return false;
		k = k - (tar *ingr[i].per - ingr[i].total <= 0 ? 0 : tar *ingr[i].per - ingr[i].total);
	}
	if (k < 0)
		return false;
	return true;
}
static bool comp(vege A, vege B) {
	return A.alre < B.alre;
}
int main()
{
	ll n, k;
	cin >> n >> k;
	vector<vege> ingr(n);
	ll per;
	for (int i = 0; i < n; ++i) {
		cin >> per;
		ingr[i].id = i;
		ingr[i].per = per;
	}
	for (int i = 0; i < n; ++i) {
		cin >> per;
		ingr[i].total = per;
		ingr[i].alre = per / ingr[i].per;
	}
	//sort(ingr.begin(), ingr.end(), comp);
	int l = 0;
	int r = ~(1 << 31);
	int res = 0;
	while (l <= r) {
		int mid = l + (r - l) / 2;
		bool co = cost(ingr, mid, k);
		if (co) {
			l = mid + 1;
			res = mid;
		}
		else {
			r = mid - 1;
		}
	
	}
	cout << res << endl;
	//system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值