标签:大根堆 str lin href 贪心 一个 pat fine name
由两行组成。
第一行包含 n个整数,表示一个可行的起飞序列,相邻两个整数用空格分隔。
输入数据保证至少存在一个可行的起飞序列。如果存在多个可行的方案,输出任意一个即可。
第二行包含 n个整数 t1, t2, „, tn,其中 ti表示航班i可能的最小起飞序号,相邻两个整数用空格分隔。
正解:贪心+堆。
第一问很简单,反向拓扑序+大根堆,然后从后往前依次填序号就行。
第二问其实也不难。和第一问一样,只要我们把当前这个点卡住,不对它进行任何操作,当我们发现堆中取出的点没有办法再标号时,那这个标号就是询问点的最小标号。
(luogu大牛分站开O2可以过)
1 //It is made by Awson on 2017.10.30 2 #include <map> 3 #include <set> 4 #include <cmath> 5 #include <ctime> 6 #include <queue> 7 #include <stack> 8 #include <cstdio> 9 #include <string> 10 #include <vector> 11 #include <cstdlib> 12 #include <cstring> 13 #include <iostream> 14 #include <algorithm> 15 #define LL long long 16 #define Max(a, b) ((a) > (b) ? (a) : (b)) 17 #define Min(a, b) ((a) < (b) ? (a) : (b)) 18 #define Abs(a) ((a) < 0 ? (-(a)) : (a)) 19 using namespace std; 20 const int N = 2000; 21 const int M = 10000; 22 23 int n, m, u, v; 24 int k[N+5], kp[N+5]; 25 struct tt { 26 int to, next; 27 }edge[M+5]; 28 int path[N+5], top; 29 int ans[N+5]; 30 struct node { 31 int x, k; 32 node() { 33 } 34 node (int _x, int _k) { 35 x = _x; k = _k; 36 } 37 bool operator < (const node &b) const { 38 return k < b.k; 39 } 40 }; priority_queue<node>Q; 41 int in[N+5]; 42 43 void add(int u, int v) { 44 edge[++top].to = v; 45 edge[top].next = path[u]; 46 path[u] = top; 47 } 48 void topsort(int lim) { 49 while (!Q.empty()) Q.pop(); 50 for (int i = 1; i <= n; i++) { 51 in[i] = kp[i]; 52 if (kp[i] == 0 && i != lim) Q.push(node(i, k[i])); 53 } 54 for (int cnt = n; cnt >= 1; cnt--) { 55 if (Q.empty()) { 56 printf("%d ", cnt); return; 57 } 58 if (Q.top().k < cnt) { 59 printf("%d ", cnt); return; 60 } 61 ans[cnt] = Q.top().x; Q.pop(); 62 for (int i = path[ans[cnt]]; i; i= edge[i].next) { 63 in[edge[i].to]--; 64 if (in[edge[i].to] == 0) { 65 if (edge[i].to != lim) Q.push(node(edge[i].to, k[edge[i].to])); 66 } 67 } 68 } 69 } 70 void work() { 71 scanf("%d%d", &n, &m); 72 for (int i = 1; i <= n; i++) scanf("%d", &k[i]); 73 for (int i = 1; i <= m; i++) { 74 scanf("%d%d", &u, &v); add(v, u); kp[u]++; 75 } 76 topsort(0); 77 for (int i = 1; i <= n; i++) printf("%d ", ans[i]); 78 printf("\n"); 79 for (int i = 1; i <= n; i++) topsort(i); 80 } 81 int main() { 82 work(); 83 return 0; 84 }
标签:大根堆 str lin href 贪心 一个 pat fine name
原文地址:http://www.cnblogs.com/NaVi-Awson/p/7768151.html