zoukankan      html  css  js  c++  java
  • 凌乱的yyy / 线段覆盖(贪心)

    https://www.luogu.org/problemnew/show/P1803  题目链接

    贪心,选择结束时间为关键字排序,相同时开始时间大的在前,然后for一遍比较就好了 

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<set>
     7 #include<vector>
     8 #include<stack>
     9 #include<queue>
    10 #include<map>
    11 using namespace std;
    12 #define ll long long
    13 #define se second
    14 #define fi first
    15 const int INF= 0x3f3f3f3f;
    16 const int N=1e6+5;
    17 
    18 int n;
    19 
    20 struct note
    21 {
    22     int a;
    23     int b;
    24 }p[N];
    25 
    26 bool cmp(note x,note y)
    27 {
    28     return (x.b<y.b || x.b==y.b&&x.a>y.a);//按b小的排,相等时按a大的排
    29 }
    30 int main()
    31 {
    32     scanf("%d",&n);
    33     for(int i=1;i<=n;i++) scanf("%d %d",&p[i].a,&p[i].b);
    34     sort(p+1,p+1+n,cmp);
    35     int cnt=1,u=p[1].b;
    36     for(int i=2;i<=n;i++)
    37     {
    38         if(p[i].a>= u)
    39             cnt++, u=p[i].b;
    40     }
    41     cout<<cnt;
    42 }
  • 相关阅读:
    java三大框架
    Servlet 工作原理解析
    Android四大基本组件介绍与生命周期
    wait 和 sleep
    Linux Mysql使用
    Android开发人员必备的10 个开发工具
    AIDL
    IPC Binder
    php 比较2字符串相似度 百分比
    php 数字 的简单加解密
  • 原文地址:https://www.cnblogs.com/thunder-110/p/9283264.html
Copyright © 2011-2022 走看看