USACO2023 FEB Silver Problem 1 Bakery (大坑:二分查找)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct order{
ll a,b,c;
};
bool check(ll m, const vector<order> &di, ll tc, ll tm){
ll n=di.size();
bool ans=true;
ll xu=tc, xd=1, yu=tm, yd=1;
for(int i=0;i<n;i++){
ll a=di[i].a, b=di[i].b, c=di[i].c;
? ? ? ?//ax+by<=c
if(a>b){//(a-b)x<=c-bm
ll t1=(c-b*m),xut;
if(t1<0){
ans=false;
break;
}
xut=t1/(a-b);
xu=min(xu,xut);
if(xu<xd){
ans=false;
break;
}
ll ydt=m-xut;
yd=max(yd,ydt);
if(yd>yu){
ans=false;
break;
}
}else{
if(a==b){
if(c-b*m<0){
ans=false;
break;
}
}else{//(b-a)y<=c-am
ll t1=(c-a*m),yut;
if(t1<0){
ans=false;
break;
}
yut=t1/(b-a);
yu=min(yu,yut);
if(yu<yd){
ans=false;
break;
}
ll xdt=m-yut;
xd=max(xd,xdt);
if(xd>xu){
ans=false;
break;
}
}
}
}
return ans;
}
int main(){
ll t;
cin>>t;
while(t>0){
t--;
ll n,tc,tm;
cin>>n>>tc>>tm;
vector<order> di(n);
for(int i=0;i<n;i++){
cin>>di[i].a>>di[i].b>>di[i].c;
}
? ? ? ?//binary search on x+y, where x is time for cookie, y is time for muffin
ll l=2,r=tc+tm;
ll m=(l+r)/2,ans=0;
while(l<=r){
if(check(m,di,tc,tm)){//pass, could increase x+y
ans=m;
l=m+1;
}else{
r=m-1;
}
m=(l+r)/2;
}
cout<<tc+tm-ans<<endl;
}
return 0;
}