HDU 5699 货物运输 二分判定

转自:http://blog.csdn.net/jtjy568805874/article/details/51480479

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=1e6+5;
const int INF=0x3f3f3f3f;
struct Node{
  int l,r;
}p[N];
int n,m;
bool judge(int x){
  int mn1=INF,mx1=-INF,mn2=INF,mx2=-INF;
  for(int i=0;i<m;++i){
    if(p[i].r-p[i].l<=x)continue;
    mn1=min(mn1,p[i].l+p[i].r+x);
    mx1=max(mx1,p[i].l+p[i].r-x);
    mn2=min(mn2,p[i].r-p[i].l+x);
    mx2=max(mx2,p[i].r-p[i].l-x);
  }
  if(mn1>=mx1&&mn2>=mx2)return true;
  return false;
}
int main(){
    while(~scanf("%d%d",&n,&m)){
       for(int i=0;i<m;++i){
        scanf("%d%d",&p[i].l,&p[i].r);
        if(p[i].l>p[i].r)swap(p[i].l,p[i].r);
       }
       int l=0,r=n;
       while(l<r){
         int mid=(l+r)>>1;
         if(judge(mid))r=mid;
         else l=mid+1;
       }
       printf("%d\n",(l+r)>>1);
    }
    return 0;
}
View Code

 

posted @ 2016-05-24 23:08  shuguangzw  阅读(142)  评论(0编辑  收藏  举报