【LA3211 训练指南】飞机调度 【2-sat】
题意
有n嫁飞机需要着陆。每架飞机都可以选择“早着陆”和“晚着陆”两种方式之一,且必须选择一种。第i架飞机的早着陆时间为Ei,晚着陆时间为Li,不得在其他时间着陆。你的任务是为这些飞机安排着陆方式,使得整个着陆计划尽量安全。话句话说,如果把所有飞机的实际着陆时间按照从早到晚的顺序排列,相邻两个着陆时间间隔的最小值(称为安全间隔)应尽量大。
分析
看到最小值最大立刻会想到二分。大体思路很好想,我们二分这个安全间隔,然后判断是否可行。那么这个题的难点就变为如何判断这个安全间隔是否可行。
n架飞机,每架飞机要么选择早起飞要么选择晚起飞,对应着2-sat问题中n个布尔型变量每个变量要么为真,要么为假。那么那m个限制条件是什么呢?
我们假设当前二分的安全间隔是P,那么如果两个时间小于P,则说明两个时间不能同时选择。比如说Ei和Lj的时间差小于P,则说明Ei和Lj不能同时选择。所以要么选择Li和Lj,要么选择Ei和Ej,要么选择Li和Ej。也就是说,xi晚起飞或者xj早起飞。到这里这个题就完全转化为了2-sat问题。
下面是AC的代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath> using namespace std;
const int maxn=+;
struct TwoSAT{
int n;
vector<int>G[*maxn];
bool mark[maxn*];
int S[maxn*],c;
bool dfs(int x){
if(mark[x^])return false;
if(mark[x])return true;
mark[x]=true;
S[c++]=x;
for(int i=;i<G[x].size();i++){
if(!dfs(G[x][i]))return false;
}
return true;
}
void init(int n){
this->n=n;
for(int i=;i<n*;i++)G[i].clear();
memset(mark,,sizeof(mark));
}
void add_clause(int x,int xval,int y,int yval){
x=x*+xval;
y=y*+yval;
G[x^].push_back(y);
G[y^].push_back(x);
} bool solve(){
for(int i=;i<n*;i+=){
if(!mark[i]&&!mark[i+]){
c=;
if(!dfs(i)){
while(c>)mark[S[--c]]=false;
if(!dfs(i+))return false;
}
}
}
return true;
}
}solver;
int n,T[maxn][];
bool test(int diff){
solver.init(n);
for(int i=;i<n;i++){
for(int a=;a<;a++){
for(int j=i+;j<n;j++){
for(int b=;b<;b++){
if(abs(T[i][a]-T[j][b])<diff)solver.add_clause(i,a^,j,b^);
}
}
}
}
return solver.solve();
}
int main(){
while(scanf("%d",&n)!=EOF&&n){
int L=,R=;
for(int i=;i<n;i++){
for(int a=;a<;a++){
scanf("%d",&T[i][a]);
R=max(R,T[i][a]);
}
}
while(L<R){
int M=L+(R-L+)/;
// cout<<L<<" "<<R<<endl;
if(test(M))L=M;
else
R=M-;
}
printf("%d\n",L);
}
return ;
}
【LA3211 训练指南】飞机调度 【2-sat】的更多相关文章
- poj 1961 Period(KMP训练指南例题)
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 11356 Accepted: 5279 Descripti ...
- 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- 【UVA11107 训练指南】Life Forms【后缀数组】
题意 输入n(n<=100)个字符串,每个字符串长度<=1000,你的任务是找出一个最长的字符串使得超过一半的字符串都包含这个字符串. 分析 训练指南上后缀数组的一道例题,据说很经典(估计 ...
- 【LA3523 训练指南】圆桌骑士 【双连通分量】
题意 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数,以防赞同和反 ...
- 训练指南 UVALive - 3126(DAG最小路径覆盖)
layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...
- 训练指南 UVALive - 3415(最大点独立集)
layout: post title: 训练指南 UVALive - 3415(最大点独立集) author: "luowentaoaa" catalog: true mathja ...
- 训练指南 UVA - 11419(二分图最小覆盖数)
layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...
- 训练指南 UVALive - 3989(稳定婚姻问题)
ayout: post title: 训练指南 UVALive - 3989(稳定婚姻问题) author: "luowentaoaa" catalog: true mathjax ...
随机推荐
- 微软Enterprise Library 4.1和Unity 1.2
说明 微软模式与实践团队今天发布了Enterprise Library 4.1和Unity 1.2版本,这次发布的主要新特性如下: 1. 支持Visual Studio 2008 SP1 2. Uni ...
- ubuntu技巧
在终端界面里面查找,命令:ctrl + shift +f VMware 中 Ubuntu 不能全屏: 转到虚拟机-->安装VMware Tools 把装载的 VMware Tools CD里面的 ...
- Linux下定时执行任务(crontab命令)
1.循环执行的计划任务 linux下面有atd和crond两种计划任务,其中,atd服务使用的at命令只能执行一次,而crond服务使用的crontab定义的命令,是循环作用的,所以crond才符合我 ...
- webpack中imports-loader,exports-loader,expose-loader的区别
Webpack有几个和模块化相关的loader,imports-loader,exports-loader,expose-loader,比较容易混淆.今天,我们来理一理. imports-loader ...
- cowboy的路由方式
直接贴代码 route_helper.erl -module(route_helper). -export([get_routes/]). get_routes() -> [ {'_', [ % ...
- Log4j配置概述
一.Log4j 简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合 ...
- redis底层数据结构--简单动态字符串 链表 字典 跳跃表 整数集合 压缩列表
1.动态字符串 redis中使用c语言的字符床存储字面量,默认字符串存储采用自己构建的简单动态字符串SDS(symple dynamic string) redis包含字符串的键值对都是用SDS实现的 ...
- css 字体英文对照
宋体: SimSun 黑体: SimHei 华文细黑: STHeiti Light [STXihei] 华文黑体: STHeiti 微软雅黑: Microsoft YaHei 微软正黑体: Micro ...
- java里面的public static void main(String[] args)
package com.java_1; public class Hello { public static void main(String[] args){ System.out.println( ...
- git 本地与远程分支冲突 解决
git pull origin master git rebase origin/master git merge origin/master git rebase --continue git pu ...