有N头牛,每头牛有个喝水时间,这段时间它将专用一个Stall
现在给出每头牛的喝水时间段,从A到B,问至少要多少个Stall 才能满足它们的要求
1 <= N <= 50,000
1 <= A <= B <= 1,000,000
保证A<=B
Format
Input
第一行给出数字N
接下来N行
每行两个数字a,b代表某头牛喝水的开始与结束时间.
Output
至少要多少个stall
Samples
输入数据 1
5
1 10
2 4
3 6
5 8
4 7
Copy
输出数据 1
4
Copy
Limitation
1s, 255024KiB for each test case.
我的代码:
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int e,b;
int w[200005];
for(int i=0;i<200005;i++){
w[i]=0; }
for(int j=0;j<n;j++){
cin>>e>>b;
for(int q=e;q<=b;q++){
w[q-1]=w[q-1]+1;
}
}
int max = 0;
for(int f=0;f<200005;f++){
if(w[f]>w[max]){
max=f;
}
}
cout<<w[max];
return 0;
}
我沿用了教师外的风景那个超时了三个的代码,结果RLE了7个,AC了八个,可能是数组的范围原因
我试了试教师外的风景别人的代码,全部AC,我需要倩倩研究一下我的代码的问题。
别人的代码(同教师外的风景):
#include<cstdio>
#include<algorithm>
using namespace std;
int n,x,y,m,a[200005];
bool cmp(int a,int b){//同一时间下回来的排前面
if (abs(a)==abs(b)) return a<b;
return abs(a)<abs(b);
}
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++){
scanf("%d%d",&x,&y);
a[++m]=x; a[++m]=-(y+1);//按规则处理出来
}
sort(a+1,a+m+1,cmp);
int sum=0,ans=0;
for (int i=1;i<=m;i++){
if (a[i]<0) sum--;//回来人数-1
else{
sum++;//出去人数+1
if (sum>ans) ans=sum;//比最大值大就替换
}
}
printf("%d",ans);
return 0;
}