求大佬帮忙说下二分check函数,或者正确思路 最好有代码拜托了!
这是我赛时代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,mod=1e9+7;
struct node
{
int id,num;
bool operator<(const node&w)const{
return id<w.id;
}
}eq[N];
int n,t,a[N],f[N],res,num,ans,m,k;
bool vis,st;
bool check(int u)
{
vis=false,st=false;
int l=1e18,r=-1e18;
for(int i=1;i<=n;i++){
if(eq[i].num<=u){
for(int j=i;j<=n;j++) l=minn(l,eq[i].num);
for(int j=i;j<=n;j++)
if(eq[i].num-l<u) return false;
return true;
}
}
return true;
}
signed main()
{
std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>eq[i].id>>eq[i].num;
sort(eq+1,eq+1+n);
int l=-1e18,r=0;
while(l<r){
int mid=l+r+1>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
cout<<r;
return 0;
}