码迷,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. long long left; 
  15. long long right; 
  16. int index; 
  17. } segment[N]; 
  18.  
  19. struct left_order 

  20. bool operator() (const segment_t& first, const segment_t& second) 
  21. { return first.left < second.left; } 
  22. }; 
  23.  
  24. struct right_order 

  25. bool operator() (const segment_t& first, const segment_t& second) 

  26. if (first.right != second.right) 
  27. return first.right > second.right; 
  28. return first.index < second.index; 

  29. }; 
  30.  
  31. struct bridge_t 

  32. long long length; 
  33. int index; 
  34. } bridge[N]; 
  35.  
  36. struct length_order 

  37. bool operator() (const bridge_t& first, const bridge_t& second) 
  38. { return first.length < second.length; } 
  39. }; 
  40.  
  41. int answer[N]; 
  42.  
  43. int main() 

  44. #ifndef ONLINE_JUDGE 
  45. freopen("input.txt", "r", stdin); 
  46. // freopen("input.txt", "w", stdout); 
  47. #endif // ONLINE_JUDGE 
  48. int n, m; 
  49. scanf("%d %d", &n, &m); 
  50. for (int i = 1; i <= n; ++i) { 
  51. scanf("%I64d %I64d", &left[i], &right[i]); 

  52. for (int i = 1; i <= m; ++i) { 
  53. scanf("%I64d", &bridge[i].length); 
  54. bridge[i].index = i; 

  55. for (int i = 1; i < n; ++i) { 
  56. segment[i].left = left[i+1] - right[i]; 
  57. segment[i].right = right[i+1] - left[i]; 
  58. segment[i].index = i; 

  59. std::sort(segment+1, segment+n, left_order()); 
  60. std::sort(bridge+1, bridge+1+m, length_order()); 
  61. std::priority_queue<segment_t, std::vector<segment_t>, right_order> pq; 
  62. int ptr = 0
  63. int assigned_count = 0
  64. for (int i = 1; i <= m; ++i) { 
  65. for (; ptr < n && segment[ptr].left <= bridge[i].length; ptr++) { 
  66. pq.push(segment[ptr]); 

  67. for (; !pq.empty() && pq.top().right < bridge[i].length; pq.pop()); 
  68. if (pq.empty()) { 
  69. continue

  70. assigned_count += 1
  71. answer[pq.top().index] = bridge[i].index; 
  72. pq.pop(); 

  73. if (assigned_count == n - 1) { 
  74. puts("Yes"); 
  75. for (int i = 1; i < n; ++i) { 
  76. printf("%d ", answer[i]); 

  77. printf("\n"); 

  78. else puts("No"); 
  79. return 0

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
迷上了代码!