【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 ...
随机推荐
- pymongo和mongodbengine之间的区别
pymongo是一个mongo driver,可以用来连接数据库以及对数据库进行操作,但是是用mongo自己的用来操作数据库的语句进行操作数据库,而mongodbengine就像是sqlalchemy ...
- riotjs 简单使用&&browserify 构建
项目地址: http://riotjs.com/ 备注: 为了简单使用了 browserify 进行构建 1. 项目结构 ├── app.css ├── gulpfile.js ├── index.h ...
- cocostudio ui编辑器 使用心得
1 c++包含路径 2九宫格设置 cocostudio ui编辑器设置九宫格x,y,w,h是从图片左上角开始测量,然后调整尺寸就行了. 2. 如果点了自适应 panel会在加载json的时候被设置 ...
- 【转】VC 模式对话框和非模式对话框的创建,销毁和区别
原文网址:http://blog.csdn.net/mycaibo/article/details/6002151 VC 模式对话框和非模式对话框的创建,销毁和区别 在WIN32中,模式对话框的创 ...
- 阿里云视频点播 php开发
先购买开通阿里云的<视频点播>服务,视频点播 可以购买套餐 ,我在项目中使用的是299套餐 开发前在<用户信息管理>生成Access Key Secret,开发密钥使用 阿里云 ...
- java根据特定密钥对字符串进行加解密
package com.test; import java.io.IOException; import java.security.SecureRandom; import javax.crypto ...
- java代码数组求平均值,最大值,最小值
(测试类) package com.badu; public class Tste { public static void main(String[] args) { Class5 sa=new C ...
- WARN deprecation:&L - HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration
WARN deprecation:&L - HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sour ...
- 【学习笔记】Manacher
扔板子跑路 代码 POJ3974 #include <cstdio> #include <cstring> #include <algorithm> using n ...
- 【UVA】673 Parentheses Balance(栈处理表达式)
题目 题目 分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差? 代码 #include <bits/stdc++.h> usin ...