经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
BZOJ2535: [Noi2010]Plane 航空管制2(拓扑排序 贪心)
来源:cnblogs  作者:自为风月马前卒  时间:2018/10/20 15:34:00  对本文有异议

题意

题目链接

Sol

非常妙的一道题。

首先不难想到拓扑排序,但是直接对原图按\(k\)从小到大拓扑排序是错的。因为当前的\(k\)大并不意味着后面的点\(k\)也大

但是在反图上按\(k\)从大到小拓扑排序就是对的。为什么呢?因为题目中给出的条件是下限, 而在反图上拓扑排序就相当于卡着下限做,因此一定是最优的

对于第二问,同样在反图上搞。对每个点分开做,贪心的策略是:如果有其他的飞机可以起飞则让他们起飞,直到没有飞机可以起飞,这时的时间就是答案

  1. // luogu-judger-enable-o2
  2. // luogu-judger-enable-o2
  3. #include<bits/stdc++.h>
  4. #define Pair pair<int, int>
  5. #define MP(x, y) make_pair(x, y)
  6. #define fi first
  7. #define se second
  8. using namespace std;
  9. const int MAXN = 2e5 + 10;
  10. inline int read() {
  11. char c = getchar(); int x = 0, f = 1;
  12. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  13. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  14. return x * f;
  15. }
  16. int N, M, a[MAXN], inder[MAXN], tmp[MAXN], ans[MAXN];
  17. vector<int> v[MAXN];
  18. void Topsort() {
  19. priority_queue<Pair> q;
  20. for(int i = 1; i <= N; i++)
  21. if(!inder[i]) q.push(MP(a[i], i));
  22. int tot = 0;
  23. while(!q.empty()) {
  24. int p = q.top().se; q.pop(); ans[++tot] = p;
  25. for(int i = 0, to; i < v[p].size(); i++) {
  26. to = v[p][i];
  27. inder[to]--;
  28. if(!inder[to]) q.push(MP(a[to], to));
  29. }
  30. }
  31. for(int i = tot; i >= 1; i--) printf("%d ", ans[i]); puts("");
  32. }
  33. int solve(int x) {
  34. memcpy(inder, tmp, sizeof(tmp));
  35. priority_queue<Pair> q;
  36. inder[x] = N;
  37. for(int i = 1; i <= N; i++) if(!inder[i]) q.push(MP(a[i], i));
  38. int tim = N;
  39. for(int i = N; i; i--) {
  40. if(q.empty() || (q.top().fi < i)) return i;
  41. int p = q.top().se; q.pop();
  42. for(int i = 0, to; i < v[p].size(); i++) {
  43. to = v[p][i];
  44. inder[to]--;
  45. if(!inder[to]) q.push(MP(a[to], to));
  46. }
  47. }
  48. return tim;
  49. }
  50. int main() {
  51. N = read(); M = read();
  52. for(int i = 1; i <= N; i++) a[i] = read();
  53. for(int i = 1; i <= M; i++) {
  54. int x = read(), y = read();
  55. v[y].push_back(x); inder[x]++; tmp[x]++;
  56. }
  57. Topsort();
  58. for(int i = 1; i <= N; i++) printf("%d ", solve(i));
  59. return 0;
  60. }
  61. /*
  62. 10 10
  63. 4 4 3 6 9 9 10 7 10 7
  64. 2 9
  65. 3 5
  66. 6 7
  67. 1 5
  68. 7 9
  69. 10 2
  70. 3 8
  71. 8 6
  72. 3 10
  73. 8 5
  74. */
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号