UVA1146 Now or later(2-sat,4级)

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 nlines. 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.


思路:二分时间间隔,选早点,还是晚点,典型的TWO-SAT问题。

         判断有互斥的,就加有向边就行了。

       用强连通,判断下就好了。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int mm=4000+9;
const int oo=1e7;
const int mn=mm*mm;
int head[mm];
class node
{
  public:int v,next;
}e[mn];
int h[mm][2];
int n;
int dfs_clock,bcc_no,dfn[mm],e_to[mm],stak[mm],top,edge;
int tarjan(int u)
{
  int lowu,lowv,v;
  lowu=dfn[u]=++dfs_clock;
  stak[++top]=u;
  for(int i=head[u];~i;i=e[i].next)
  {
    v=e[i].v;
    if(!dfn[v])
    {
      lowv=tarjan(v);
      lowu=min(lowu,lowv);
    }
    else if(!e_to[v])
      lowu=min(dfn[v],lowu);
  }
  if(lowu==dfn[u])
  { ++bcc_no;
    while(1)
    {v=stak[top--];
     e_to[v]=bcc_no;
     if(v==u)break;
    }
  }
  return lowu;
}
void bcc_find()
{
   memset(dfn,0,sizeof(dfn));
   memset(e_to,0,sizeof(e_to));
   top=dfs_clock=bcc_no=0;
   int z=n+n;
   for(int i=0;i<z;++i)
    if(!dfn[i])tarjan(i);

}
void data()
{
  memset(head,-1,sizeof(head));edge=0;
}
void add(int u,int v)
{
  e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
}
inline int fabs(int x)
{
  if(x<0)return -x;return x;
}
bool jud(int u,int uu,int v,int vv,int x)
{
  if(fabs(h[u][uu]-h[v][vv])<x)
  return 1;
    return 0;
}
bool judge(int mid)
{ data();
  for(int i=0;i<n;++i)
    for(int ii=0;ii<2;++ii)
    for(int j=i+1;j<n;++j)
    for(int jj=0;jj<2;++jj)
    if(jud(i,ii,j,jj,mid))
    add(i+i+ii,j+j+jj^1),add(j+j+jj,i+i+ii^1);
  bcc_find();/**
  puts("eeeeee");
  for(int i=0;i<n+n;++i)
    printf("%d %d\n",i,e_to[i]);
    puts("eeeeee--");*/
  for(int i=0;i<n;++i)
    if(e_to[i+i]==e_to[i+i+1])return 0;
  return 1;
}
int main()
{ int a,b;
  while(~scanf("%d",&n))
  {   int l=0,r=0;
      int ans=0,mid;
     for(int i=0;i<n;++i)
      {scanf("%d%d",&h[i][0],&h[i][1]);
       r=max(r,max(h[i][0],h[i][1]));
      }
      while(l<=r)///二分间隔
      {
         mid=(l+r)/2;
         if(judge(mid)){l=mid+1;ans=max(ans,mid);}
         else r=mid-1;
      }
      printf("%d\n",ans);
  }
  return 0;
}




你可能感兴趣的:(UVA1146 Now or later(2-sat,4级))