UVA 1146 Now or later

二分时间+2sat

边加多了....RE了好久......


Now
or later

Time Limit: 9000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

As you must have experienced, instead of landing immediately, an aircraft sometimes waits in a holding loop close to the runway. This holding mechanism is required by air traffic controllers to space apart aircraft
as much as possible on the runway (while keeping delays low). It is formally defined as a ``holding pattern‘‘ and is a predetermined maneuver designed to keep an aircraft within a specified airspace (see Figure 1 for an example).

Figure 1: A simple Holding Pattern as described in a pilot text book.

Jim Tarjan, an air-traffic controller, has asked his brother Robert to help him to improve the behavior of the airport.

Input

The input file, that contains all the relevant data, contains several test cases

Each test case is described in the following way. The first line contains the number n of aircraft ( 2n2000).
This line is followed by n lines. Each of these lines contains two integers, which represent the early landing time and the late landing time of an aircraft. Note that all times t are such
that 0t107.

Output

For each input case, your program has to write a line that conttains the maximal security gap between consecutive landings.

Sample Input

10
44 156
153 182
48 109
160 201
55 186
54 207
55 165
17 58
132 160
87 197

Sample Output

10

Note: The input file corresponds to Table 1.

Robert‘s Hints

Optimization vs. Decision
Robert advises you to work on the decision variant of the problem. It can then be stated as follows: Given an integer p, and an instance of the optimization problem, the question is to decide if there is a solution with
security gap p or not. Note that, if you know how to solve the decision variant of an optimization problem, you can build a binary search algorithm to find the optimal solution.

On decision
Robert believes that the decision variant of the problem can be modeled as a very particular boolean satisfaction problem. Robert suggests to associate a boolean variable per aircraft stating whether the aircraft is early (variable takes value
``true‘‘) or late (value ``false‘‘). It should then be easy to see that for some aircraft to land at some time has consequences for the landing times of other aircraft. For instance in Table 1 and with a delay of 10, if aircraft A1 lands
early, then aircraft A3 has to land late. And of course, if aircraft A3 lands early, then aircraft A1 has to land late.
That is, aircraft A1 and A3 cannot both land early and formula (A1  ?A3 (A3  ?A1) must
hold.

And now comes Robert‘s big insight: our problem has a solution, if and only if we have no contradiction. A contradiction being something like Ai  ?Ai.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=4010;
struct Edge
{
    int to,next;
}edge[maxn*maxn*4];

int Adj[maxn],Size;

void init()
{
    Size=0; memset(Adj,-1,sizeof(Adj));
}

void Add_Edge(int u,int v)
{
    edge[Size].to=v;
    edge[Size].next=Adj[u];
    Adj[u]=Size++;
}

int Low[maxn],DFN[maxn],Instack[maxn],Belong[maxn];
int Index,Stack[maxn],top,scc;

void Tarjan(int u)
{
    DFN[u]=Low[u]=++Index;
    Stack[top++]=u; Instack[u]=1;
    int v;

    for(int i=Adj[u];~i;i=edge[i].next)
    {
        v=edge[i].to;
        if(!DFN[v])
        {
            Tarjan(v);
            Low[u]=min(Low[v],Low[u]);
        }
        else if(Instack[v])
        {
            Low[u]=min(Low[u],DFN[v]);
        }
    }

    if(DFN[u]==Low[u])
    {
        scc++;
        do
        {
            v=Stack[--top];
            Instack[v]=0;
            Belong[v]=scc;
        }while(v!=u);
    }
}

bool ck(int n)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,0,sizeof(Instack));
    Index=scc=top=0;

    for(int i=0;i<n;i++)
    {
        if(!DFN[i]) Tarjan(i);
    }

    for(int i=0;i<n;i+=2)
    {
        if(Belong[i]==Belong[i^1]) return false;
    }
    return true;
}

int air[maxn][2],n;

bool test(int mid)
{
    init();
    for(int i=0;i<n;i++)
    {
        for(int k1=0;k1<2;k1++)
        {
            for(int j=i+1;j<n;j++)
            {
                for(int k2=0;k2<2;k2++)
                {
                    if(abs(air[i][k1]-air[j][k2])<mid)
                    {
                        Add_Edge(i*2+k1,j*2+1-k2);
                        Add_Edge(j*2+k2,i*2+1-k1);
                    }
                }
            }
        }
    }
    return ck(n*2);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&air[i][0],&air[i][1]);
        }
        int ans=-1,low=0,high=1e7+10,mid;
        while(low<=high)
        {
            mid=(low+high)/2;
            if(test(mid)) ans=mid,low=mid+1;
            else high=mid-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

UVA 1146 Now or later,布布扣,bubuko.com

时间: 2024-11-03 16:31:11

UVA 1146 Now or later的相关文章

UVA 1146 - Now or later(2-SET)

UVA 1146 - Now or later 题目链接 题意:n个飞机,每个飞机有一个早到时间和一个晚到时间,问怎么安排飞机,使得飞机到的间隔的最小值最大 思路:二分答案,然后利用2-set去判断,如果两个飞机的两个时刻间隔比这个时刻小,那么就是表示不能同时满足这两个条件,就加一条xi^xj的边进去,然后利用2-SET判定一下 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include &l

uva 1146(2-sat)

刚开始学,具体题解看紫书325页 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <set> using namespace std; #define hh printf("=======================

uva 1146 Now or late (暴力2-SAT)

/* 裸地2-SAT问题 关键是模型转化 最小的最大 显然二分 关键是Judge的时候怎么判断 每个航班是早是晚直接影响判断 早晚只能选一个 如果我们定义bool变量xi表示 i航班是否早到 每个航班虚拟出两个点2*i 2*i+1 分别表示是否早到 然后就可以假设某个航班早到然后推导出一定连得某些边 然后就开始选点 尝试这个点选不选 看看最后是否合法 */ #include<iostream> #include<cstdio> #include<cstring> #in

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica