Topic
For N Islands and M-bridges, ask if the bridge is enough to connect 22 neighboring islands, and connect them when the length of a bridge is greater than or equal to the minimum distance of points on the neighboring two islands and is less than or equal to the maximum distance from the points on the neighboring two islands.
Using the greedy method, the island is sorted by the minimum distance (i.e. L[i+1]-r[i]) and the bridge is sorted by length, and it is obvious that the smallest island should be connected with the smallest bridge in length, which is the first greedy. The second greedy is that the current island can be connected to all bridges should be selected distance maximum value of the smallest. Think carefully about the two greedy ideas are right. Finally see if all the islands are connected together.
Code:
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath > #include <climits> #include <algorithm> #include <vector> #include <string> #include < iostream> #include <sstream> #include <map> #include <set> #include <queue> #include < stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> Using namespace std, #define PB push_back#define MP make_pair#define REP (i,x,n) for (int i=x;i< (n); ++i) #define for (I,l, h) for (int i= (l); i<= (h), ++i) #define FORD (i,h,l) for (int i= (h); i>= (l); i) #define SZ (x) ((int) (x). Size ()) #define All (x) (x). Begin (), (x). End () #define RI (x) scanf ("%d", & (x)) #define RII (x, Y) scanf ("%d%d", & (X), & (Y)) # Define RIII (x, Y, z) scanf ("%d%d%d", & (X), & (Y), & (z)) #define DRI (x) int (x); scanf ("%d", &x) #define DRII (x, y) int X, y; scanf ("%d%d", &x, &y) #define DRIII (x, y, z) int x, y, Z; scanf ("%d%d%d", &x, &y, &z) #define OI (x) printf ("%d", x), #define RS (x) scanf ("%s", (x)) #define MS0 (x) memset ( (x), 0, sizeof ((x))) #define MS1 (x) memset ((x),-1, sizeof ((x))) #define LEN (x) strlen (x) #define F first#define S second#def Ine Swap (A, B) (a ^= B, b ^= A, a ^= b) #define Dpoint strcut node{int x, y} #define CMPD int cmp (const int &A,CONST int &B) {return a>b;}/* #ifdef HOME freopen ("In.txt", "R", stdin); #endif */const int MOD = 1e9+7;typedef vector<int> vi;typedef vector<string> vs;typedef vector<double> Vd;typedef Long Long ll;typedef pair<int,int> pii;//#define Homeint Scan () {int res = 0, ch, flag = 0;if (ch = getcha R ()) = = '-')//determine positive and negative flag = 1;else if (ch >= ' 0 ' && ch <= ' 9 ')//Get complete number res = CH-' 0 '; while ((ch = getchar ()) > = ' 0 ' && ch <= ' 9 ') res = res * + CH-' 0 '; return flag? -res:res;}/*----------------Please-----does-----not-----HACK-----ME--------------------*/struct node{long Long int l,r; int id; BOOL operator< (Node B) const {return r<b.r; } bool Operator> (Node B) const {return r>b.r; }}; Node isl[200000+5];struct a{long long int num; int id;}; A a[200000+5];int ans[200000+5];bool cmp (const node& a,const Node &b) {return A.L<B.L;} BOOL Cmp1 (const A&a,const a&b) {return a.num<b.num;} priority_queue<node,vector<node>,greater<node> >q;int Main () {int n,m; RII (n,m); long long int l1,l2,r1,r2;scanf ("%i64d%i64d", &L1,&R1); REP (i,1,n) {scanf ("%i64d%i64d", &l2,&r2); isl[i].l=l2-r1;isl[i].r=r2-l1;l1=l2;r1=r2;isl[i].id=i;} REP (i,1,m+1) {scanf ("%i64d", &a[i].num); A[i].id=i;} Sort (isl+1,isl+n,cmp), sort (A+1,A+M+1,CMP1), int i,j;int flag=0;for (i=1,j=1;i<=m;i++) {while (J<N&&ISL [J].l<=a[i].num) {Q.push (isl[j]); j + +; } if (Q.empty ()) continue; Node Tt=q.top (); Q.pop (); if (tt.r>=a[i].num) {ans[tt.id]=a[i].id; } else {flag=1; Break }}if (flag| | j<n| |! Q.empty ()) {printf ("no\n");} else{printf ("yes\n"); REP (i,1,n-1) {printf ("%d", ans[i]); } printf ("%d\n", Ans[n-1]);} return 0;}
Codeforce 310 Div2 D case of fugitive