[Topic link]
Https://codeforces.com/contest/496/problem/E
Algorithm
Sort by right endpoint, each song takes precedence over the left end of the largest performer
With std:: set to maintain greed
Complexity of Time: O (NLOGN)
Code
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e5 +Ten;Const intINF =2e9;intN, M;structinfo{intL, R, K, home; BOOLtype;} A[MAXN<<1];Set< pair<int,pair<int,int> > >s;intans[maxn];template<typename t> InlinevoidChkmax (T &x,t y) {x =Max (x, y);} Template<typename t> InlinevoidChkmin (T &x,t y) {x =min (x, y);} Template<typename t> InlinevoidRead (T &x) {T F=1; x =0; Charc =GetChar (); for(;!isdigit (c); c = GetChar ())if(c = ='-') F =-F; for(; IsDigit (c); c = GetChar ()) x = (x <<3) + (x <<1) + C-'0'; X*=F;} InlineBOOLCMP (Info a,info b) {if(A.R! = B.R)returnA.R >B.R; Else returnA.type >B.type;}intMain () {read (n); for(inti =1; I <= N; i++) { intL, R; scanf ("%d%d",&l,&R); A[i]= (info) {l,r,0Ifalse}; } read (M); for(inti =1; I <= m; i++) { intL, R, K; scanf ("%d%d%d",&l,&r,&k); A[n+ i] = (info) {l,r,k,i,true}; } sort (A+1, A + (n + m) +1, CMP); for(inti =1; I <= n + m; i++) { if(A[i].type) {S.insert (Make_pair (A[i].l,make_pair (A[i].k,a[i].home))); Continue; } Else{S.insert (Make_pair (A[i].l,make_pair (Inf,inf))); Set< pair<int,pair<int,int> > >:: Iterator it =S.find (Make_pair (A[i].l,make_pair (Inf,inf))); if(It = =S.begin ()) {printf ("no\n"); return 0; } It--; Pair<int,pair<int,int> > TMP = (*it); S.erase (IT); Ans[a[i].home]=Tmp.second.second; if(Tmp.second.first >1) S.insert (Make_pair (Tmp.first,make_pair (Tmp.second.first-1, Tmp.second.second))); It=S.find (Make_pair (A[i].l,make_pair (Inf,inf))); S.erase (IT); }} printf ("yes\n"); for(inti =1; I <= N; i++) printf ("%d", Ans[i]); printf ("\ n"); return 0; }
[Codeforces 496E] Distributing Parts