UVA1146 Now or later(2-sat,4级)
来源:互联网 发布:我想学计算机编程 编辑:程序博客网 时间:2024/05/16 12:39
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).
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
1044 156153 18248 109160 20155 18654 20755 16517 58132 16087 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级)
- 【UVA1146】NOW OR LATER 2-SAT问题
- uva1146 - Now or later
- UVA1146 Now or later
- UVA1146 Now or later
- LA 3211 Now or later / 2-SAT
- LA3211--Now or later(2-sat)
- UVALive3211- Now or later(二分+2-SAT)
- UVALive - 3211 Now or later (2-SAT)
- UVAlive3211 Now or later(2-SAT)
- La3211 Now or later 2-Sat
- UVA 1146 Now or later(2-SAT)
- Now or later UVA - 1146 2-sat
- UVa 1146 Now or Later (2-SAT)
- La3211 Now or Later(2-SAT判定+二分法)
- UVa 1146 - Now or later(2-SAT + 二分)
- UVA 1146 Now or later(2-SAT问题)
- Uva 1146 - Now or later 二分+构图2-sat判断
- POJ题目分类
- 外链是我们次要的来流
- servlet中如何获得pageContext
- Android开发入门之采用SharedPreferences保存用户偏好设置参数
- 独立经济体——货币基础
- UVA1146 Now or later(2-sat,4级)
- 杭电1256-画8
- 外链的做用是不成轻忽的
- 如何共享同一个局域网中某台电脑中的文件
- Ride解题报告
- iOS OurChoice折叠特效实现
- 做SEO推广从心态开始
- 网站建设的几个建议
- JAVA正则表达式 Pattern和Matcher