★【贪心法】【NOI2010】航空管制

【问题描述】
世博期间,上海的航空客运量大大超过了平时,随之而来的航空管制也频频发生。最近,小X就因为航空管制,连续两次在机场被延误超过了两小时。对此,小X表示很不满意。
在这次来烟台的路上,小X不幸又一次碰上了航空管制。于是小X开始思考关于航空管制的问题。
假设目前被延误航班共有n个,编号为1至n。机场只有一条起飞跑道,所有的航班需按某个顺序依次起飞(称这个顺序为起飞序列)。定义一个航班的起飞序号为该航班在起飞序列中的位置,即是第几个起飞的航班。
起飞序列还存在两类限制条件:
第一类(最晚起飞时间限制):编号为i的航班起飞序号不得超过ki;
第二类(相对起飞顺序限制):存在一些相对起飞顺序限制(a, b),表示航班a的起飞时间必须早于航班b,即航班a的起飞序号必须小于航班b的起飞序号。
小X思考的第一个问题是,若给定以上两类限制条件,是否可以计算出一个可行的起飞序列。第二个问题则是,在考虑两类限制条件的情况下,如何求出每个航班在所有可行的起飞序列中的最小起飞序号。
【输入格式】
输入文件plane.in第一行包含两个正整数n和m,n表示航班数目,m表示第二类限制条件(相对起飞顺序限制)的数目。
第二行包含n个正整数k1, k2, …, kn。
接下来m行,每行两个正整数a和b,表示一对相对起飞顺序限制(a, b),其中1≤a,b≤n, 表示航班a必须先于航班b起飞。
【输出格式】
输出文件plane.out由两行组成。
第一行包含n个整数,表示一个可行的起飞序列,相邻两个整数用空格分隔。输入数据保证至少存在一个可行的起飞序列。如果存在多个可行的方案,输出任意一个即可。
第二行包含n个整数t1, t2, …, tn,其中ti表示航班i可能的最小起飞序号,相邻两个整数用空格分隔。
【如何评分】
如果你的输出文件格式与题目要求不符,则得0分。即你的输出文件必须满足:第一行恰好包含n个整数,且第二行也恰好包含n个整数。
当你的输出文件格式与题目要求相符时:
1. 如果仅第一行正确,获得对应测试点40%的分数;
2. 如果仅第二行正确,获得对应测试点60%的分数;
3. 如果两行均正确,获得对应测试点100%的分数。
【样例输入1】
5 5
4 5 2 5 4
1 2
3 2
5 1
3 4
3 1
【样例输出1】
3 5 1 4 2
3 4 1 2 1
【样例输入2】
5 0
3 3 3 5 5
【样例输出2】
3 2 1 5 4
1 1 1 4 4
【样例说明】
在样例1 中:
起飞序列3 5 1 4 2满足了所有的限制条件,所有满足条件的起飞序列有:
3 4 5 1 2      3 5 1 2 4      3 5 1 4 2      3 5 4 1 2
5 3 1 2 4      5 3 1 4 2      5 3 4 1 2
由于存在(5, 1)和(3, 1)两个限制,航班1只能安排在航班5和3之后,故最早起飞时间为3,其他航班类似。
在样例2 中:
虽然航班4、5没有相对起飞顺序限制,但是由于航班1、2、3都必须安排在前3个起飞,所以4、5最早只能安排在第4个起飞。
【数据范围】
对于30%数据:n≤10;
对于60%数据:n≤500;
对于100%数据:n≤2,000,m≤10,000。
【运行时限】
1秒。
【运行空限】
512M。
咋一看题,似乎无从下手,于是写了一个搜索,可惜过不了第二问,只得了30%的数据中的12分。

然后找题解,大家都说这是一道贪心,仔细一想发现好像也确实是————所有飞机,只需要尽可能晚地起飞,腾出时间让其它需要在更早时间内起飞的飞机起飞就可以了……

于是这就把第一问解决了。

对于第二问,开始是想就直接统计每个点的前驱有多少,发现这样不行,因为这不是树形图,直接统计的话会有重叠部分。看了题解之后,发现实际上就是先让其它飞机尽量晚飞,那么不能飞的时候就该这个飞机起飞了。

Accode:

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>

using namespace std;
const int maxN = 2010;

struct Edge
{
	int v; Edge *next; Edge() {}
	Edge(int v, Edge *next): v(v), next(next) {}
} *edge[maxN];
struct heap
{
	pair <int, int> hp[maxN << 2]; int T;
	heap(): T(0) {}
	heap &operator=(const heap &b)
	{
		T = b.T;
		memcpy(hp, b.hp, sizeof hp);
		return *this;
	}
	void ad_down()
	{
		for (int i = 1; (i <<= 1) <= T;)
		{
			if (i < T && hp[i] < hp[i + 1]) ++i;
			if (hp[i >> 1] < hp[i]) swap(hp[i], hp[i >> 1]);
			else break;
		}
		return;
	}
	void ad_up()
	{
		for (int i = T; i >> 1; i >>= 1)
		if (hp[i >> 1] < hp[i]) swap(hp[i], hp[i >> 1]);
		else break;
		return;
	}
	void push(pair <int, int> p)
	{hp[++T] = p; ad_up(); return;}
	pair <int, int> top() const {return hp[1];}
	void pop() {hp[1] = hp[T--]; ad_down(); return;}
	bool empty() const {return !T;}
} hp, hp1;
bool marked[maxN], locked[maxN];
int route[maxN], Lim[maxN], cnt1[maxN];
int res[maxN], cnt[maxN], ans[maxN], n, m;

inline void update(int u)
{
	for (Edge *p = edge[u]; p; p = p -> next)
	if (!(--cnt[p -> v]))
		hp.push(make_pair(Lim[p -> v], p -> v));
	return;
}
//飞机u起飞后,将它所指向的飞机的入度减1,
//若有新的飞机入度为零,那么加入堆中。

inline void update1(int u)
{
	for (Edge *p = edge[u]; p; p = p -> next)
	if (!(--cnt1[p -> v]))
		hp1.push(make_pair(Lim[p -> v], p -> v));
	return;
}
//飞机u起飞后,将它所指向的飞机的入度减1,
//若有新的飞机入度为零,那么加入堆中。

int main()
{
	freopen("plane.in", "r", stdin);
	freopen("plane.out", "w", stdout);
	scanf("%d%d", &n, &m);
	for (int i = 1; i < n + 1; ++i)
		scanf("%d", Lim + i);
	while (m--)
	{
		int u, v; scanf("%d%d", &u, &v);
		edge[v] = new Edge(u, edge[v]);
		++cnt[u]; ++cnt1[u];
		//反向建边,统计入度(反过来的入度)。
	}
	for (int i = 1; i < n + 1; ++i)
		if (!cnt[i]) hp.push(make_pair(Lim[i], i));
	//cnt用来统计每个点的入度,若入度为零,代表可以直接起飞,
	//加入堆中,每次让最晚时间限制最宽松的飞机最晚起飞。
	for (int i = n; i; --i)
	{
		int u = hp.top().second, tmp, j = i; hp.pop();
		hp1 = hp; res[i] = u; memcpy(cnt1, cnt, sizeof cnt);
		//res用于记录起飞顺序。
		while (!hp1.empty() && Lim[tmp = hp1.top().second] >= j)
			hp1.pop(), update1(tmp), --j;
		ans[u] = j;
		//以上循环部分是处理第二问的情况,用一个临时的cnt1和hp1,
		//先让其它飞机尽量晚起飞,然后不能起飞时就是飞机u的最早起飞时间,
		//记录在ans中。
		update(u);
	}
	for (int i = 1; i < n + 1; ++i)
		printf("%d ", res[i]);
	printf("\n");
	for (int i = 1; i < n + 1; ++i)
		printf("%d ", ans[i]);
	printf("\n");
	return 0;
}
第一二问单独处理的贪心程序:


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>

using namespace std;
const int maxN = 2010;

struct Edge
{
	int v; Edge *next; Edge() {}
	Edge(int v, Edge *next): v(v), next(next) {}
} *edge[maxN];
priority_queue<pair <int, int> > hp;
bool marked[maxN], locked[maxN];
int route[maxN], Lim[maxN], cnt0[maxN];
int res[maxN], cnt[maxN], n, m;

inline void renew(int u)
{
	for (Edge *p = edge[u]; p; p = p -> next)
	if (!(--cnt[p -> v]))
		hp.push(make_pair(Lim[p -> v], p -> v));
	return;
}

int main()
{
	freopen("plane.in", "r", stdin);
	freopen("plane.out", "w", stdout);
	scanf("%d%d", &n, &m);
	for (int i = 1; i < n + 1; ++i)
		scanf("%d", Lim + i);
	while (m--)
	{
		int u, v; scanf("%d%d", &u, &v);
		edge[v] = new Edge(u, edge[v]);
		++cnt[u]; ++cnt0[u];
	}
	for (int i = 1; i < n + 1; ++i)
		if (!cnt[i]) hp.push(make_pair(Lim[i], i));
	for (int i = n; i; --i)
	{
		int u = hp.top().second; hp.pop();
		res[i] = u;
		renew(u);
	}
	for (int i = 1; i < n + 1; ++i)
		printf("%d ", res[i]);
	printf("\n");
	for (int i = 1; i < n + 1; ++i)
	{
		while (!hp.empty()) hp.pop();
		for (int j = 1; j < n + 1; ++j)
		if (!(cnt[j] = cnt0[j]))
			hp.push(make_pair(Lim[j], j));
		for (int j = n; j; )
		{
			pair <int, int> ths = hp.top(); hp.pop();
			int u = ths.second; 
			if (u == i)
			{
				int tmp;
				while (!hp.empty() && Lim[tmp = hp.top().second] >= j)
					hp.pop(), renew(tmp), --j;
				printf("%d ", j); break;
			}
			--j;
			renew(u);
		}
	}
	printf("\n");
	return 0;
}

你可能感兴趣的:(struct,测试,UP,HP,2010,pair)