Gift Set 二分 数学

在这里插入图片描述
题意:
给定x,y个红,蓝气球,每次可以选择a个红气球,b个蓝气球,或者a个蓝气球,b个红气球,问最多能分成多少组。
思路:
不妨设a>b,不难发现这个题可以二分,即如果ans = x ,那么对于任意y<x都会存在方案让答案有解 假设第一种方案有k组,那么k要满足又要大于等于0并且小于等于当前组数,然后又有两个柿子生成,ak+b(mid-k)<=x && bk+a(mid-k)<=y,由此可以产生关于k的解,那么题目就转变为给你两个线段,两条线段是否相交。
这里有个坑点就是判断线段,如果不整除左端点要向上,右端点要向下。如果右端点是负数,还得减1,wa死我了。
代码:

// Problem: G. Gift Set
// Contest: Codeforces - Codeforces Round #725 (Div. 3)
// URL: https://codeforces.com/contest/1538/problem/G
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
typedef long long ll; 
using namespace std;
#define int long long
int x,y,a,b;
bool check(int mid)
{
	int l=ceill((a*mid-y)*1.0l/(a-b) );
	int r=(x-b*mid)/(a-b);
	if(x-b*mid<0)
	return 0;
	if(l>r)		return 0;
	int maxl=max(l,0ll);
	int minr=min(r,mid);
	return maxl<=minr;
	
}
signed main()
{
	ios;
	int t;
	cin >> t;
	while(t--)
	{
	
		cin >> x >> y >> a >> b;
		if(a<b)
		{
			swap(a,b);
			swap(x,y);
		}
		if(a==b){
			cout<<min(x,y)/a<<endl;
			continue;
		}
		int l = 0,r=1e9+100;
		while(l<r)
		{
			int mid=l+r+1>>1;
			if(check(mid))	l=mid;
			else	r=mid-1;
		}
		cout<<l<<endl;
	}
	
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值