hdu 5699 货物运输 (啥)

http://acm.hdu.edu.cn/showproblem.php?pid=5699

中文题

二分答案,对每个区间可以求得x和y的范围,看有没有解

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;


#define eps 1e-9
#define LL long long 
#define ULL unsigned long long
#define pii pair<int,int>
#define MP make_pair
#define N (2000000 + 10)
#define M (3000000 + 10)
#define inf 0x3f3f3f3f
#define ls i << 1
#define rs i<<1|1
#define lson ls, ll, md
#define rson rs, md+1, rr


int l[N], r[N];
int n, m;
bool check(int mid) {
    int mi1 = inf, mi2 = inf, ma1 = -inf, ma2 = -inf;
    bool e = 1;
    for(int i = 0; i < m; ++i) {
        if(r[i]-l[i] > mid) {
            ma1 = max(ma1, r[i]-l[i] - mid);
            mi1 = min(mi1, r[i]-l[i] + mid);
            mi2 = min(mi2, l[i]+r[i] + mid);
            ma2 = max(ma2, l[i]+r[i] - mid);
            e = 0;
        }
    }
    if(e) return 1;
    if(mi1 == ma1 && mi2 == ma2) {
        if((mi1 + mi2)&1) return 0;
    }
    return mi1 >= ma1 && mi2 >= ma2;
}

int main() {
    while(~scanf("%d%d", &n ,&m)) {
        for(int i = 0; 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+1;
        int ans = 0 ;
        while(l < r) {
            int mid = l+r>>1;
            if(check(mid)) r = mid, ans = mid;
            else l = mid+1;
        }
        printf("%d\n",ans);
    }
}

阅读更多
上一篇Codeforces Round #351 (VK Cup 2016 Round 3, Div. 1 Edition) C E (斜率优化. 概率)
下一篇hdu 5664 Lady CA and the graph (树分治,树状数组)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭