【codevs1214】线段覆盖

problem

solution

codes

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 110;
int a[maxn], b[maxn], c[maxn];
bool cmp(int x, int y){
    return b[x]==b[y] ? a[x]<a[y] : b[x]<b[y];
}
int main(){
    int n;  cin>>n;
    for(int i = 1; i <= n; i++){
        int x, y;  cin>>x>>y;  //输入有坑,可能先大再小
        a[i]=min(x,y);  b[i]=max(x,y);  c[i]=i;
    }
    sort(c+1,c+n+1,cmp);
    //for(int i = 1; i <= n; i++)cout<<a[c[i]]<<" "<<b[c[i]]<<"
";
    int t = -999, ans = 0;
    for(int i = 1; i <= n; i++){
        if(t <= a[c[i]]){
            t = b[c[i]];  ans++;
        }
    }
    cout<<ans<<"
";
    return 0;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444700.html