中级启示者
排序后
for(int i=1;i<=n;i++){
if(a[i].st>=x){
ans++;
x=a[i].end;
}
else if(a[i].st>=x2){
ans++;
x2=a[i].end;
}
if(x<x2) swap(x,x2);
}
修练者
- struct ke{
- int s,e;
- }a[205];
- bool cmp(ke a,ke b){
- return a.e<b.e;
- }
- int main()
- {
- int n;
- cin>>n;
- for(int i=1;i<=n;i++)
- cin>>a[i].s>>a[i].e;
- sort(a+1,a+1+n,cmp);
- int x=-1,ans=0,x2=-1;
- for(int i=1;i<=n;i++){
- if(a[i].s>=x&&a[i].s>=x2){
- ans++;
- if(x>x2)x=a[i].e;
- else x2=a[i].e;
- }
- else if(a[i].s>=x&&a[i].s<x2){
- ans++;
- x=a[i].e;
- }
- else if(a[i].s<x&&a[i].s>=x2){
- ans++;
- x2=a[i].e;
- }
- }
- cout<<ans;
- }
资深守护
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<cstring>
#include<sstream>
using namespace std;
struct ke{
int s,e;
}a[105];
bool cmp(ke a,ke b){
return a.e<b.e;
}
int main(){
//freopen("3742.in","r",stdin);
//freopen("3742.out","w",stdout);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].s>>a[i].e;
}
sort(a+1,a+n+1,cmp);
int x=-1,ans=0,x2=-1;
for(int i=1;i<=n;i++){
if(a[i].s>=x&&a[i].s>=x2){
ans++;
if(x>x2) x=a[i].e;
else x2=a[i].e;
}
else if(a[i].s>=x&&a[i].s<x2){
ans++;
x=a[i].e;
}
else if(a[i].s<x&&a[i].s>=x2){
ans++;
x2=a[i].e;
}
}
cout<<ans;
return 0;
//fclose(stdin);
//fclose(stdout);
}