AcWing 4905. 面包店 二分

类似还有一个题是二分,用区间来判断是否有解

这个爆long long 有点坑了 

const int N = 1e2 + 10;


LL n,tc,Tm;
LL a[N],b[N],c[N];

bool check(LL mid)
{
	LL minx = max(0LL,mid + 1 - Tm),maxx = min(tc - 1LL,mid);//将y转为x的函数,此时判断x是否有解
	//枚举所有客户的需求,不断对x的范围取交集
	for (int i = 1;i <= n;i ++)
	{
		//爆Long long
		// LL t1 = b[i] - a[i],t2 = c[i] - a[i] * tc - b[i] * (Tm - mid);
		// if (t1 == 0 && t2 < 0) return false;	
		// else if (t1 > 0) maxx = min(maxx,(LL)floor(t2 / t1));
		// else if (t1 < 0) minx = max(minx,(LL)ceil(t2 / t1));
		
		double t1 = b[i] - a[i],t2 = c[i] - (double)a[i] * tc - (double)b[i] * (Tm - mid);
		if (t1 == 0 && t2 < 0) return false;	
		else if (t1 > 0) maxx = min(maxx,(LL)floor(t2 / t1));
		else if (t1 < 0) minx = max(minx,(LL)ceil(t2 / t1));

	}
	return minx <= maxx;
}

void solve()
{
	cin >> n >> tc >> Tm;
	for (int i = 1;i <= n;i ++) cin >> a[i] >> b[i] >> c[i];
	
	//二分总共升级多少
	LL l = -1,r = tc + Tm;
	while(l + 1 != r)
	{
		LL mid = l + r >> 1;
		if (check(mid)) r = mid;
		else l = mid;
	}
	
	if (check(l)) cout << l << endl;
	else cout << r << endl;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值