First of all the left is to be selected, and then the right to note that some of the interval can be removed. 1. There are two different aces in the interval. 2. There is an A in the interval, and this is not a corresponding to this interval.
I was wrong in the first place on the 2nd judgment condition, I define the ID record is the last occurrence position, can not be judged, so simply in the structure to record his corresponding a value.
The interval left after the drop can be sorted according to the boundary of the interval, and then the intersection is obtained.
In general, the greedy mind is not difficult, but there are some details to pay attention to.
The code is as follows:
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<vector>#include<algorithm>#include<queue>#include<Set>#include<map>using namespacestd;Const intN = 1e5+5;Const intM = 1e6+6;Set<int>St;Set<int>:: Iterator It;map<int,int>ID;structEdge {intL,r,a,ok; voidSet (intXintYinta) {L=x; R=y; A=A; OK=0; }} E[n];intSum[m];BOOLCMP (Edge A,edge b) {if(A.L! = B.L)returnA.L <B.L; returnA.R <B.R;}intMain () {//freopen ("F.in.cpp", "R", stdin); intT,n,a,b,c,ans,ca=0, max,min,cnt; scanf ("%d",&T); while(t--) {scanf ("%d",&N); St.clear (); Id.clear (); Max= -1; Min=1e8; for(inti =1; I <= N; i++) {scanf ("%d%d%d",&a,&b,&c); Id[a]=i; E[i]. Set (B,c,a); St.insert (a); Max=Max (Max (max,a), E[I].R); Min=min (min (min,a), E[I].L); } CNT=0; for(inti = min-1; I <= Max; i++) { if(Id[i]) cnt++; Sum[i]=CNT; } ans=st.size ();//printf ("ans1 =%d\n", ans); for(inti =1; I <= N; i++) { if(SUM[E[I].R]-sum[e[i].l-1] ==1) {It=St.lower_bound (E[I].L); if(*it! = E[i]. A) E[i].ok =1; } if(SUM[E[I].R]-sum[e[i].l-1] >1) E[i].ok =1; } sort (E+1, e+n+1, CMP); inttr=-1; for(inti =1; I <= N; i++) { if(E[i].ok = =1)Continue; if(tr = =-1|| TR <E[I].L) {TR=E[I].R; Ans++; } Else if(TR >=E[I].L) {TR=min (e[i].r,tr); }} printf ("Case #%d:%d\n",++Ca,ans); } return 0;}
Uvalive 6911 Double Swords (Set, greedy, seek interval intersection)