区间贪心

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3fffffff
const int maxn=1010;
struct node{
    int x,y;
}N[maxn];
bool cmp(node a,node b){
    if(a.x!=b.x){
        return a.x>b.x;
    }else{
        return a.y<b.y;
    }
}
//区间贪心
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        scanf("%d %d",&N[i].x,&N[i].y);
    }
    sort(N,N+n,cmp);
    int first=N[0].x;
    cout<<"不相交的区间:"<<endl;
    cout<<N[0].x<<" "<<N[0].y<<endl;
    int num=1;
    for(int i=1;i<n;i++){
        if(N[i].y<=first){
            cout<<N[i].x<<" "<<N[i].y<<endl;
            first=N[i].x;
            num++;
        }
    }
    cout<<num<<endl;
    return 0;
}
//输入
//4
//1 3
//2 4
//3 5
//4 6
//输出
//不相交的区间:
//4 6
//2 4
//2

 

posted @ 2021-04-15 22:09  XA科研  阅读(49)  评论(0)    收藏  举报