HDU5699 2016"百度之星" - 初赛(Astar Round2B)货物运输

二分答案判定

四条不等式简单推一下吧

#include 
#include 
#include 
#define INF 2147483647
#define N 1000050

using namespace std;
int l[N],r[N],n,m;


bool check(int x)
{
    int mi1 = INF , mi2 = INF , ma1 = -INF , ma2 = -INF;
    for (int i=1;i<=m;i++) if (r[i] - l[i] > x) 
    {
        mi1 = min(mi1,r[i] + l[i] + x);
        ma1 = max(ma1,r[i] + l[i] - x);
        mi2 = min(mi2,r[i] - l[i] + x);
        ma2 = max(ma2,r[i] - l[i] - x);
    }
    if(ma1 <= mi1 && ma2 <= mi2)  
    {  
        if(ma1 == mi1 && ma2 == mi2)  
        {  
            if((ma1 + ma2) & 1)  
                return false;  
            return true;  
        }  
        return true;  
    }  
    return false;  
}

int main()
{
    while (~scanf("%d%d",&n,&m))
    {
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        for (int i=1;i<=m;i++) {
            scanf("%d%d",&l[i],&r[i]);
            if (l[i] > r[i]) swap(l[i],r[i]);
        }
        int l = 0 , r = n;
        while (l < r)
        {
            int mid = (l + r) / 2;
            if (check(mid) == true) r = mid; else l = mid + 1;
        }
        printf("%d\n",l);
    }
    return 0;
}


你可能感兴趣的:(HDU5699 2016"百度之星" - 初赛(Astar Round2B)货物运输)