E xin trình bày tư tưởng bài này:
Mỗi lần nhập giá trị a[i].x , nếu là đầu mút trái thì a[i].y=1, còn phải thì a[i].y=2.
Sau đó em sort theo a[i].x.
Từ đây em duyện mảng 2*n. Nếu gặp a[i].y =1 thì cho biến đếm++. Nếu a[i].y=2 thì đếm–.
Nếu đếm > kết quả và đếm >1 thì ta lưu kết quả = max(kết quả,đếm)
Em nộp bài 0 điểm và không biết lí do, mong mn giúp đỡ.
Code:
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e6+5;
struct sapxep{
int x,y;
};
sapxep a[maxn];
bool ss(sapxep q,sapxep p){
if(q.x==p.x) return q.y>p.y;
return q.x<p.x;
}
int n,ans=0,s=0;
int main(){
//freopen("INSEG.inp","r",stdin);
//freopen("INSEG.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n*2;i++)
{
scanf("%d",&a[i].x);
a[i].y=1;
scanf("%d",&a[++i].x);
a[i].y=2;
}
sort(a+1,a+n*2+1,ss);
for(int i=1;i<=2*n;i++){
if(a[i].y==1) s++;
else if(a[i].y==2) s--;
if(s>ans && s>1) ans=s;
}
printf("%d",ans);
}