https://www.acwing.com/problem/content/5442/
t数组就是他们的排序,如果有0个比i大,那么就是最大,排好序后写不等式
hi + (n) * ai > hi+1 + (n) * ai+1
const int N = 1e6 + 10;
struct node
{
int t, h, a;
}st[N];
int cmp(node x, node y)
{
return x.t < y.t;
}
void solve()
{
int n; cin >> n;
for (int i = 1; i <= n; i++) cin >> st[i].h;
for (int i = 1; i <= n; i++) cin >> st[i].a;
for (int i = 1; i <= n; i++) cin >> st[i].t;
sort(st + 1, st + 1 + n,cmp);
int l = 0, r = 1e18;
int ok = 1;
for (int i = 1; i <= n - 1; i++)
{
if (st[i].a < st[i + 1].a)
{
if (st[i].h <= st[i + 1].h)
{
ok = 0;
break;
}
}
if (st[i].a == st[i + 1].a)
{
if (st[i].h <= st[i + 1].h)
{
ok = 0;
break;
}
}
else
{
if (st[i].a > st[i + 1].a)
{
l = max(l, (int)floor(1.0*(st[i + 1].h - st[i].h) / (st[i].a - st[i + 1].a)) + 1);//下取整加一,做到了即使是整除也要加一
}
else
{
r = min(r, (int)ceil(1.0 * (st[i].h - st[i + 1].h) / (st[i + 1].a - st[i].a)) - 1);//上取整减一,做到了即使整除也要减一
}
}
}
if (l > r || ok == 0) cout << "-1" << '\n';
else cout << l << '\n';
}
https://www.acwing.com/problem/content/description/4908/
不等式有两个未知数,而升级数越大用时越短,成单调性,所以二分相当于加多了一条方程(x+y=mid),变成了两个未知数两条方程,可解
const int N=1e6+10;
int a[N],b[N],c[N];
int n,x,y;
int cheak(int ok)
{
int l=max((int)0,ok+1-y);
int r=min(ok,x-1);
for(int i=1;i<=n;i++)
{
int cnt=b[i]-a[i];
//int ans=a[i]-b[i];
int res=c[i]-a[i]*x-b[i]*y+b[i]*ok;
if(cnt==0&&res<0) return 0;
else if(cnt>0) r=min(r,(int)floor(1.0*res/cnt));
else l=max(l,(int)ceil(1.0*res/cnt));
}
if(l<=r) return 1;
else return 0;
}
void solve()
{
cin>>n>>x>>y;
for(int i=1;i<=n;i++) cin>>a[i]>>b[i]>>c[i];
int l=0,r=x+y-2;
int daan=0;
while(l<=r)
{
int mid=(l+r)/2;
if(cheak(mid)) daan=mid,r=mid-1;
else l=mid+1;
}
cout<<daan<<endl;
}