zoukankan      html  css  js  c++  java
  • CODEVS 1214

    1214 线段覆盖

    题意:中文题

    思路:贪心,先按l排序后按r排序,每次标记当前剩下线段的最右端,因为l是从小到达有序的,所以不需要考虑左端点,只需要考虑右边尽量往左端点取就是了

    AC代码:

    #include "iostream"
    #include "string.h"
    #include "stack"
    #include "queue"
    #include "string"
    #include "vector"
    #include "set"
    #include "map"
    #include "algorithm"
    #include "stdio.h"
    #include "math.h"
    #define ll long long
    #define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
    #define mem(a) memset(a,0,sizeof(a))
    #define mp(x,y) make_pair(x,y)
    #define pb(x) push_back(x)
    using namespace std;
    const long long INF = 1e18+1LL;
    const int inf = 1e9+1e8;
    const int N=1e5+100;
    const ll mod=1e9+7;
    
    struct Node{
        int l, r;
        bool friend operator< ( Node a, Node b){
            if(a.l==b.l) return a.r<b.r;
            return a.l<b.l;
        }
    };
    Node arr[105];
    int n,ans=-1;
    int main(){
        ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
        cin>>n;
        for(int i=1; i<=n; ++i){
            int x,y;
            cin>>x>>y;
            arr[i].l=min(x,y);
            arr[i].r=max(x,y);
        }
        sort(arr+1,arr+1+n);
        int p=inf;
        for(int i=1; i<=n; ++i){
            if(arr[i].r>=p){
                if(arr[i].l<p){
                    ans++;
                }
                else p=arr[i].r;
            }
            else{
                ans++;
                p=arr[i].r;
            }
        }
        cout<<n-ans<<"
    ";
        return 0;
    }
  • 相关阅读:
    JavaScript 操作CSS
    源码搭建LAMP服务器
    Modified 2 color sort
    python的网络库
    找出有序整数数组中下标与值相同的所有元素
    sql 查看Oralce 数据库连接状态
    oracle 快闪 sql
    Sql server dblink
    昆山桶装水/免费送货上门/
    C# Tostring() 格式大全 [转]
  • 原文地址:https://www.cnblogs.com/max88888888/p/7224058.html
Copyright © 2011-2022 走看看