Topic Connection:
Http://codeforces.com/problemset/problem/555/B
Main topic:
There are N Islands (islands in a column, can be seen as linear, used to describe the location of the island is the starting point and end point), M bridge, given the location of each island and the length of the bridge, ask if you can connect the N islands?
Problem Solving Ideas:
Sort + greedy, for N Islands, the starting and ending points of the adjacent two can be converted to the length range of the N-1 bridge, arranging the interval in ascending order.
For the M-Bridge ascending arrangement, for each bridge enumeration can be legally covered by the interval, select the best, the time to meet the l<bridge_length<r,l is ordered. We just need to select the smallest of R, because the larger the R, the more bridges the interval can accommodate.
The soft soft for the selection of the optimal interval when the priority queue is not the best way to handle, but the soft soft is soft, but growing_up.
1#include <algorithm>2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cstdlib>6#include <cmath>7#include <queue>8 using namespacestd;9 Ten Const intMAXN =200010; One #defineLL __int64 A structBridge - { - LL Val, id; the BOOL operator< (ConstBridge &a)Const - { - returnVal <A.val; - } + } BRI[MAXN]; - + struct Island A { at LL R, l, id; - BOOL operator< (ConstIsland &a)Const - { - returnL <A.L; - } - } IS[MAXN]; in structnode - { to LL s, id; + BOOL operator< (ConstNode &a)Const - { the returns >A.S; * } $ };Panax Notoginseng - LL VIS[MAXN], N, m; the BOOLSolve () + { APriority_queue <node>Q;//the current bridge can build a successful interval the intL=0; +memset (Vis,0,sizeof(Vis)); - $ for(intI=0; i<m; i++)//selection of optimal interval construction for each bridge $ { - node Nu; - while(!q.empty ()) the { -Nu =q.top ();Wuyi if(Nu.s <bri[i].val) theQ.pop ();//Delete an illegal interval in a queue - Else Wu Break; - } About $ while(BRI[I].VAL>=IS[L].L && is[l].r >= bri[i].val && l<n-1) - { -Nu.id =is[l].id; -Nu.s =IS[L].R; AQ.push (NU);//interval Add-in queue +L + +; the } - if(Q.empty ()) $ Continue;//if there is no suitable edge, continue to cycle the edge theNu =q.top (); theVis[nu.id] = bri[i].id;//record the edges corresponding to the connection interval the Q.pop (); the } - for(intI=1; i<n; i++) in if(!Vis[i]) the return false; the return true;//all the intervals are to the proper side About } the intMain () the { the while(SCANF ("%i64d%i64d", &n, &m)! =EOF) + { - Island Pre, cur; the LL i;Bayiscanf ("%i64d%i64d", &PRE.L, &PRE.R); the for(i=1; i<n; i++) the { -scanf ("%i64d%i64d", &CUR.L, &CUR.R); -is[i-1].id =i; theis[i-1].L = CUR.L-PRE.R; theis[i-1].R = CUR.R-Pre.l; thePre =cur; the } - for(i=0; i<m; i++) the { theBri[i].id = i+1; thescanf ("%i64d", &bri[i].val);94 } theSort (IS, is+n-1); theSort (Bri, bri+m); the if(Solve ())98 { Aboutprintf ("yes\n"); - for(i=1; i<n-1; i++)101printf ("%i64d", Vis[i]);102printf ("%i64d\n", Vis[i]);103 }104 Else theprintf ("no\n");106 }107 return 0;108}
Codeforces 555B Case of fugitive