/*The first thing I thought was an interval covering and then knocking. Submission 60 points 0.0 However, I felt that the data was wrong and found out that xxx his barn is a lap (the cow is very good AH also can admire the lake ...) And then enumerate the broken points unfortunately n=750 p=10000 this set of data in the 1.3-second way, 90 minutes later, I can read the puzzle and find out what it's like. 0.0 Below is a two-time code*/#include<iostream>#include<cstdio>#include<algorithm>#defineMAXN 10010using namespacestd;structnode{intL,r;} BOY[MAXN];intcmpConstNode &x,ConstNode &y) { if(X.L==Y.L)returnx.r<Y.R; returnx.l<Y.L;}intn,p,k,ans,minn=99999999;intMain () {scanf ("%d%d",&n,&p); intx, y; for(intI=1; i<=p;i++) {scanf ("%d%d",&x,&y); BOY[I].L=x;boy[i].r=y; } for(k=1; k<=n;k++) {ans=0; for(intI=1; i<=p;i++) { if(boy[i].l<k) boy[i].l+=N; if(boy[i].r<k) boy[i].r+=N; if(boy[i].l>boy[i].r) Swap (BOY[I].L,BOY[I].R); } sort ( boy+1, boy+1+p,cmp); intri=boy[1].r,li=boy[1].L; for(intI=2; i<=p;i++) if(boy[i].l>RI) {ans=ans+ri-Li; Li=BOY[I].L; RI=BOY[I].R; } ElseRi=Max (RI,BOY[I].R); Ans=ans+ri-Li; Minn=min (Minn,ans); } printf ("%d", Minn); return 0;}/*and check Set*/#include<iostream>#include<cstdio>#include<cstring>#defineMAXN 10010using namespacestd;intfa[maxn],l[maxn],r[maxn],n,p,ans,ans=99999999;intFindintx) { if(X!=fa[x]) fa[x]=find (fa[x]); returnfa[x];}voidUnionn (intXinty) { if(x>y) Swap (x, y); for(inti=x;i<y;i++) if(Find (i)!=find (i+1) ) {ans++; Fa[find (i)]=find (i+1); }}intMain () {CIN>>n>>p; for(intI=1; i<=p;i++) Cin>>l[i]>>R[i]; for(intk=1; k<=n;k++) {ans=0; for(intI=1; i<=n*2; i++) fa[i]=i; for(intI=1; i<=p;i++) { intTmp1=l[i],tmp2=R[i]; if(tmp1<k) tmp1=tmp1+N; if(tmp2<k) tmp2=tmp2+N; if(Find (TMP1)! =find (TMP2)) Unionn (Find (TMP1), find (TMP2)); } Ans=min (Ans,ans); } cout<<Ans; return 0;}
codevs1955 Optical fiber communication (and look up set)