隐藏
「NOI2010」航空管制 - 贪心 | Bill Yang's Blog

路终会有尽头,但视野总能看到更远的地方。

0%

「NOI2010」航空管制 - 贪心

题目大意


题目分析

不妨考虑倒着贪心。
对于任意方案:显然可以让起飞的航班尽可能靠后,在堆中每次选出$k$最大的航班起飞。
对于$i$的最小起飞序号:不让$i$入队,强制选择其他入度为$0$的航班,若不能继续弹出其他航班,那么当前位置就是$i$的最小起飞序号。


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;

inline const int Get_Int() {
int num=0,bj=1;
char x=getchar();
while(x<'0'||x>'9') {
if(x=='-')bj=-1;
x=getchar();
}
while(x>='0'&&x<='9') {
num=num*10+x-'0';
x=getchar();
}
return num*bj;
}

const int maxn=2005;

int n,m,k[maxn],InDegree[maxn],tmp[maxn],Ans[maxn],cnt=0;
vector<int>edges[maxn];

void AddEdge(int x,int y) {
edges[x].push_back(y);
}

struct HeapNode {
int k,u;
HeapNode(int _k=0,int _u=0):k(_k),u(_u) {}
bool operator < (const HeapNode& b) const {
return k<b.k;
}
};

int Solve(int x) {
memcpy(tmp,InDegree,sizeof(InDegree));
tmp[x]=n;
priority_queue<HeapNode>Q;
for(int i=1; i<=n; i++)
if(!tmp[i])Q.push(HeapNode(k[i],i));
for(int i=n; i>=1; i--) {
if(Q.empty()||Q.top().k<i)return i;
int Now=Q.top().u;
Q.pop();
for(int Next:edges[Now]) {
tmp[Next]--;
if(!tmp[Next])Q.push(HeapNode(k[Next],Next));
}
}
}

int main() {
n=Get_Int();
m=Get_Int();
for(int i=1; i<=n; i++)k[i]=Get_Int();
for(int i=1; i<=m; i++) {
int x=Get_Int(),y=Get_Int();
AddEdge(y,x);
InDegree[x]++;
}
memcpy(tmp,InDegree,sizeof(InDegree));
priority_queue<HeapNode>Q;
for(int i=1; i<=n; i++)
if(!tmp[i])Q.push(HeapNode(k[i],i));
while(!Q.empty()) {
int Now=Q.top().u;
Q.pop();
Ans[++cnt]=Now;
for(int Next:edges[Now]) {
tmp[Next]--;
if(!tmp[Next])Q.push(HeapNode(k[Next],Next));
}
}
for(int i=cnt; i>=1; i--)printf("%d ",Ans[i]);
putchar('\n');
for(int i=1; i<=n; i++)printf("%d ",Solve(i));
return 0;
}
姥爷们赏瓶冰阔落吧~