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 <vector>
#include <algorithm>
using namespace std;

const int MAXNODE = 2005;

struct TwoSet {
	int n;
	vector<int> g[MAXNODE * 2];
	bool mark[MAXNODE * 2];
	int S[MAXNODE * 2], sn;

	void init(int tot) {
		n = tot * 2;
		for (int i = 0; i < n; i += 2) {
			g[i].clear();
			g[i^1].clear();
		}
		memset(mark, false, sizeof(mark));
	}

	void add_Edge(int u, int uval, int v, int vval) {
		u = u * 2 + uval;
		v = v * 2 + vval;
		g[u^1].push_back(v);
		g[v^1].push_back(u);
	}

	bool dfs(int u) {
		if (mark[u^1]) return false;
		if (mark[u]) return true;
		mark[u] = true;
		S[sn++] = u;
		for (int i = 0; i < g[u].size(); i++) {
			int v = g[u][i];
			if (!dfs(v)) return false;
		}
		return true;
	}

	bool solve() {
		for (int i = 0; i < n; i += 2) {
			if (!mark[i] && !mark[i + 1]) {
				sn = 0;
				if (!dfs(i)){
					for (int j = 0; j < sn; j++)
						mark[S[j]] = false;
					sn = 0;
					if (!dfs(i + 1)) return false;
				}
			}
		}
		return true;
	}
} gao;

const int N = 2005;

int n, ti[N][2];
int L, R;

bool judge(int len) {
	gao.init(n);
	for (int i = 0; i < n; i++) {
		for (int a = 0; a < 2; a++) {
			for (int j = i + 1; j < n; j++) {
				for (int b = 0; b < 2; b++) {
					if (abs(ti[i][a] - ti[j][b]) < len)
						gao.add_Edge(i, a^1, j, b^1);
				}
			}
		}
	}
	return gao.solve();
}

int main() {
	while (~scanf("%d", &n)) {
		L = R = 0;
		for (int i = 0; i < n; i++)
			for (int j = 0; j < 2; j++) {
				scanf("%d", &ti[i][j]);
				R = max(R, ti[i][j]);
			}
		R++;
		while (L < R) {
			int mid = (L + R) / 2;
			if (judge(mid)) L = mid + 1;
			else R = mid;
		}
		printf("%d\n", L - 1);
	}
	return 0;
}
时间: 2024-12-15 08:59:11

UVA 1146 - Now or later(2-SET)的相关文章

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

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