悬关,站外题,简单二分
  • 板块灌水区
  • 楼主SakurajiamaMai
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/28 18:55
  • 上次更新2023/11/2 11:01:50
查看原帖
悬关,站外题,简单二分
784813
SakurajiamaMai楼主2023/10/28 18:55

求大佬帮忙说下二分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;
}


2023/10/28 18:55
加载中...