码迷,mamicode.com
首页 > 其他好文 > 详细

Round 310(Div.1) B. Case of Fugitive

时间:2016-05-18 21:44:02      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

Round 310(Div.1) B. Case of Fugitive

Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water.

The only dry land there is an archipelago of n narrow islands located in a row. For more comfort let‘s represent them as non-intersecting segments on a straight line: island i has coordinates [li,?ri], besides, ri?<?li+1 for 1?≤?i?≤?n?-?1.

To reach the goal, Andrewid needs to place a bridge between each pair of adjacent islands. A bridge of length a can be placed between the i-th and the (i?+?1)-th islads, if there are such coordinates of x and y, that li?≤?x?≤?ri, li?+?1?≤?y?≤?ri?+?1 and y?-?x?=?a.

The detective was supplied with m bridges, each bridge can be used at most once. Help him determine whether the bridges he got are enough to connect each pair of adjacent islands.

  1. #include <stdio.h> 
  2. #include <string.h> 
  3.  
  4. #include <queue> 
  5. #include <vector> 
  6. #include <algorithm> 
  7.  
  8. const int N = 200000 + 5; 
  9.  
  10. long long left[N]; 
  11. long long right[N]; 
  12.  
  13. struct segment_t 
  14. { 
  15. long long left; 
  16. long long right; 
  17. int index; 
  18. } segment[N]; 
  19.  
  20. struct left_order 
  21. { 
  22. bool operator() (const segment_t& first, const segment_t& second) 
  23. { return first.left < second.left; } 
  24. }; 
  25.  
  26. struct right_order 
  27. { 
  28. bool operator() (const segment_t& first, const segment_t& second) 
  29. { 
  30. if (first.right != second.right) 
  31. return first.right > second.right; 
  32. return first.index < second.index; 
  33. } 
  34. }; 
  35.  
  36. struct bridge_t 
  37. { 
  38. long long length; 
  39. int index; 
  40. } bridge[N]; 
  41.  
  42. struct length_order 
  43. { 
  44. bool operator() (const bridge_t& first, const bridge_t& second) 
  45. { return first.length < second.length; } 
  46. }; 
  47.  
  48. int answer[N]; 
  49.  
  50. int main() 
  51. { 
  52. #ifndef ONLINE_JUDGE 
  53. freopen("input.txt", "r", stdin); 
  54. // freopen("input.txt", "w", stdout); 
  55. #endif // ONLINE_JUDGE 
  56. int n, m; 
  57. scanf("%d %d", &n, &m); 
  58. for (int i = 1; i <= n; ++i) { 
  59. scanf("%I64d %I64d", &left[i], &right[i]); 
  60. } 
  61. for (int i = 1; i <= m; ++i) { 
  62. scanf("%I64d", &bridge[i].length); 
  63. bridge[i].index = i; 
  64. } 
  65. for (int i = 1; i < n; ++i) { 
  66. segment[i].left = left[i+1] - right[i]; 
  67. segment[i].right = right[i+1] - left[i]; 
  68. segment[i].index = i; 
  69. } 
  70. std::sort(segment+1, segment+n, left_order()); 
  71. std::sort(bridge+1, bridge+1+m, length_order()); 
  72. std::priority_queue<segment_t, std::vector<segment_t>, right_order> pq; 
  73. int ptr = 0; 
  74. int assigned_count = 0; 
  75. for (int i = 1; i <= m; ++i) { 
  76. for (; ptr < n && segment[ptr].left <= bridge[i].length; ptr++) { 
  77. pq.push(segment[ptr]); 
  78. } 
  79. for (; !pq.empty() && pq.top().right < bridge[i].length; pq.pop()); 
  80. if (pq.empty()) { 
  81. continue; 
  82. } 
  83. assigned_count += 1; 
  84. answer[pq.top().index] = bridge[i].index; 
  85. pq.pop(); 
  86. } 
  87. if (assigned_count == n - 1) { 
  88. puts("Yes"); 
  89. for (int i = 1; i < n; ++i) { 
  90. printf("%d ", answer[i]); 
  91. } 
  92. printf("\n"); 
  93. } 
  94. else puts("No"); 
  95. return 0; 
  96. } 

Round 310(Div.1) B. Case of Fugitive

标签:

原文地址:http://www.cnblogs.com/gu-castle/p/5506555.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!