hdu5699货物运输

来源:互联网 发布:凯斯西储大学轴承数据 编辑:程序博客网 时间:2024/05/01 14:13

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

题意:中文题。

分析:因为只能建一个传送站并且要求最大的时间最小,我们二分答案,但是怎么判断能?设当前答案为x建立传送站的位置为L,R,显然只有r-l>x的运送方案才会需要使用传送站且会满足|l-L|+|r-R|<=x。那么我们能得出2个不等式组:(1)l+r+x>=L+R>=l+r-x。(2)l-r+x>=L-R>=l-r-x。我们只需要判断这两个不等式组中的L和R是否有解就行了,之前做过一个类似的题是Codeforces Round #359 (Div. 2)E. Optimal Point

代码:

#include<map>#include<set>#include<cmath>#include<queue>#include<bitset>#include<math.h>#include<vector>#include<string>#include<stdio.h>#include<cstring>#include<iostream>#include<algorithm>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;const int N=1000010;const int mod=100000000;const int MOD1=1000000007;const int MOD2=1000000009;const double EPS=0.00000001;typedef long long ll;const ll MOD=1000000007;const int MAX=1000000010;const ll INF=1ll<<55;const double pi=acos(-1.0);typedef double db;typedef unsigned long long ull;struct node {    int l,r;}a[N];int m,mi1,mx1,mi2,mx2;int pd(int x) {    int i,l1,l2,r1,r2;    mx1=mx2=-MAX;mi1=mi2=MAX;    for (i=1;i<=m;i++)    if (a[i].r-a[i].l>x) {        mi1=min(mi1,a[i].l+a[i].r);        mi2=min(mi2,a[i].l-a[i].r);        mx1=max(mx1,a[i].l+a[i].r);        mx2=max(mx2,a[i].l-a[i].r);    }    l1=mx1-x;r1=mi1+x;    l2=mx2-x;r2=mi2+x;    if (l1>r1||l2>r2) return 0;    if ((l1+l2+1)/2>(r1+r2)/2) return 0;    if ((l1-r2+1)/2>(r1-l2)/2) return 0;    return 1;}int main(){    int i,l,r,n,mid;    while (scanf("%d%d", &n, &m)!=EOF) {        for (i=1;i<=m;i++) {            scanf("%d%d", &a[i].l, &a[i].r);            if (a[i].l>a[i].r) swap(a[i].l,a[i].r);        }        l=-1;r=n;mid=(l+r)>>1;        while (l+1<r)        if (pd(mid)) { r=mid;mid=(l+r)>>1; }        else { l=mid;mid=(l+r)>>1; }        printf("%d\n", r);    }    return 0;}


0 0
原创粉丝点击