zoukankan      html  css  js  c++  java
  • 905. 区间选点(贪心)

       分析:先按区间的右端点排序,然后从左到右遍历,当前区间左端点小于上一区间的右端点,总个数不变,否则总个数加一,并更新右端点

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 100010;
    struct Edge{int a, b;} edges[N];
    bool operator < (Edge e1, Edge e2) {
        return e1.b < e2.b;
    }
    int n;
    int main() {
        scanf("%d",&n);
        for(int i = 0; i < n; i++) {
            int a, b;
            scanf("%d%d",&a,&b);
            edges[i] = {a,b};
        }
        sort(edges,edges+n);
        int res = 1, pre = edges[0].b;
        for(int i = 1; i < n; i++) {
            int a = edges[i].a, b = edges[i].b;
            if(a <= pre) {
                continue;
            }
            res++;
            pre = b;
        }
        printf("%d
    ",res);
        return 0;
    }
  • 相关阅读:
    【题解】B进制星球
    高斯消元
    7.16
    题解 P1572 【计算分数】
    7.30
    7.31
    项目中使用 MyBatis(一)
    从整体上了解Spring MVC
    Spring注解
    Spring IOC 和 DI
  • 原文地址:https://www.cnblogs.com/yonezu/p/13563589.html
Copyright © 2011-2022 走看看