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

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. #include <queue> 

  4. #include <vector> 

  5. #include <algorithm> 


  6. const int N = 200000 + 5; 


  7. long long left[N]; 

  8. long long right[N]; 


  9. struct segment_t 



  10. long long left; 

  11. long long right; 

  12. int index; 

  13. } segment[N]; 


  14. struct left_order 



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

  16. { return first.left < second.left; } 

  17. }; 


  18. struct right_order 



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



  20. if (first.right != second.right) 

  21. return first.right > second.right; 

  22. return first.index < second.index; 



  23. }; 


  24. struct bridge_t 



  25. long long length; 

  26. int index; 

  27. } bridge[N]; 


  28. struct length_order 



  29. bool operator() (const bridge_t& first, const bridge_t& second) 

  30. { return first.length < second.length; } 

  31. }; 


  32. int answer[N]; 


  33. int main() 



  34. #ifndef ONLINE_JUDGE 

  35. freopen("input.txt", "r", stdin); 

  36. // freopen("input.txt", "w", stdout); 

  37. #endif // ONLINE_JUDGE 

  38. int n, m; 

  39. scanf("%d %d", &n, &m); 

  40. for (int i = 1; i <= n; ++i) { 

  41. scanf("%I64d %I64d", &left[i], &right[i]); 



  42. for (int i = 1; i <= m; ++i) { 

  43. scanf("%I64d", &bridge[i].length); 

  44. bridge[i].index = i; 



  45. for (int i = 1; i < n; ++i) { 

  46. segment[i].left = left[i+1] - right[i]; 

  47. segment[i].right = right[i+1] - left[i]; 

  48. segment[i].index = i; 



  49. std::sort(segment+1, segment+n, left_order()); 

  50. std::sort(bridge+1, bridge+1+m, length_order()); 

  51. std::priority_queue<segment_t, std::vector<segment_t>, right_order> pq; 

  52. int ptr = 0; 

  53. int assigned_count = 0; 

  54. for (int i = 1; i <= m; ++i) { 

  55. for (; ptr < n && segment[ptr].left <= bridge[i].length; ptr++) { 

  56. pq.push(segment[ptr]); 



  57. for (; !pq.empty() && pq.top().right < bridge[i].length; pq.pop()); 

  58. if (pq.empty()) { 

  59. continue; 



  60. assigned_count += 1; 

  61. answer[pq.top().index] = bridge[i].index; 

  62. pq.pop(); 



  63. if (assigned_count == n - 1) { 

  64. puts("Yes"); 

  65. for (int i = 1; i < n; ++i) { 

  66. printf("%d ", answer[i]); 



  67. printf("\n"); 



  68. else puts("No"); 

  69. return 0; 



时间: 2024-12-22 10:03:05

Round 310(Div.1) B. Case of Fugitive的相关文章

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门 1 /* 2 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 3 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数, 4 其他是2次操作,还要减去k-1个娃娃是只要套上就可以 5 详细解释:http://blog.csdn.net/firstlucker/article/details/46671251 6 */ 7 #include <cstdio> 8 #i

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

题目传送门 1 /* 2 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 3 构造:先求出使第1个指向0要多少步,按照这个次数之后的能否满足要求 4 题目读的好累:( 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #i

#310 (div.2) D. Case of Fugitive

1.题目描述:点击打开链接 2.解题思路:本题利用贪心法+优先队列解决.不过本题的贪心策略的选取是关键,有些看似正确的贪心策略实际上暗含危险.先说说正确的贪心策略:将所有的岛按照顺序求出第i个岛和i+1个岛之间桥的最小最大长度,并按照L从小到大排序,若相同则按照R从小到大排序.然后对桥由小到大排序,将所有的桥扫描一遍,枚举第i个桥时,将L值小于等于当前桥的区间按照(R,id)放入优先队列,R小的在队首,大的在队尾,每次看队首的R是否大于等于len,若满足,则记录答案,break,若不存在,则无解

Codeforces Round #310 (Div. 1) C. Case of Chocolate (线段树)

题目地址:传送门 这题虽然是DIV1的C..但是挺简单的..只要用线段树分别维护一下横着和竖着的值就可以了,先离散化再维护.每次查找最大的最小值<=tmp的点,可以直接在线段树里搜,也可以二分去找. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.

Codeforces Round #310 (Div. 1) C Case of Chocolate

思路:对于每个点而言.只与它相邻的两个点有关系.所以可以用stl或者线段树来找到它的相邻点. 代码:187ms(开挂之后貌似是最快的- -) #include <cstdio> #include <map> #include <algorithm> using namespace std; const int N = 200000 + 1; int x[N], y[N], t[N]; //适用于正负整数 template <class T> inline b

codeforces Round #310(Div.1) 题解

嘴巴选手真爽,一不用打代码二不用掉Rating三还可以打杂.... 感觉这套题不难,但是被出题人出瞎了... 555A. Case of Matryoshkas 题目大意:给定n个大小从1到n的套娃,初始套成k坨,每次你可以选择两个操作: 1.选择一个不在任何其他套娃里的套娃,将里面的套娃取出来(要求原先里面有套娃) 2.选择一个不再任何其他套娃里的套娃,将一个套娃塞进去(要求原先里面没有套娃) 求将所有套娃合并的最小操作次数 如果一个套娃x初始在最里面,或者满足大小为1...x?1的套娃都在它

codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)

题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元组[ll,rr]排序(ll相同时再rr),长度x排序(升序).一个全局优先队列pq(rr小的顶部).for循环,对每个x,将ll比它小的放入优先队列pq,如果pq仍为空,说明这块桥用不上,不为空,看top的rr是否大于x,如果大于,这块桥就能用上,并且给当前的top一定是可行的. 乱码: #pragma co

Codeforces Round #310 (Div. 1)——A水——Case of Matryoshkas

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls