Uvalive 6911 Double Swords (Set, greedy, seek interval intersection)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.