考试订错3---1
方法---暴力
#include<bits/stdc++.h> using namespace std; int n,L,R,ansl,ansr,ans; double s; bool rope[10009]; int main() { //freopen("dormitory.in","r",stdin); //freopen("dormitory.out","w",stdout); cin>>n>>s; cin>>L>>R; for(register int i=1;i<n;i++) { int l,r; scanf("%d%d",&l,&r); if(l<L)l=L;//*省去我冗杂的判断 if(r>R)r=R; for(register int j=l;j<=r;j++) { rope[j]=1; } } for(int i=L;i<=R;i++) { int ll=i; while(rope[i]==0&&i<=R)i++; int rr=i-1; if(rr-ll+1>ans)//直接得到区间,不必另算,i++的应用注意限制i<=r,不然会错40分 { ans=rr-ll+1; ansl=ll; ansr=rr; } } if(ans==0)cout<<"0 0"<<endl; else cout<<ansl<<" "<<ansr<<endl; return 0; }
我可怜巴巴的40分代码
#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<map> using namespace std; int l1,r1,l,r,n,cnt=0; int re=0,rr,rl; int a[10005]; double s; const int MAXN=999999999; int main() { freopen("dormitory.in","r",stdin); freopen("dormitory.out","w",stdout); int i,j,f=0; cin>>n>>s; cin>>l1>>r1; r1=min(int(s),r1); for(i=1;i<=n-1;i++) { cin>>l>>r; if(l>=l1&&r<=r1) { for(j=l;j<=r;j++) {a[j]=MAXN;} } else if(l>=l1&&r>r1) { for(j=l;j<=r1;j++) {a[j]=MAXN;} } else if(l<l1&&r<=r1) { for(j=l1;j<=r;j++) {a[j]=MAXN;} }//改掉此点可得六十分 } for(i=l1;i<=r1;i++) { if(a[i]!=MAXN) { cnt++; if(cnt==1) {l=i;} continue; } if(cnt>re){ r=i-1; re=cnt; cnt=0; rr=r; rl=l; } } if(rl!=rr) cout<<rl<<" "<<rr<<endl; else cout<<"0"<<" "<<"0"<<endl; return 0; }