zoukankan      html  css  js  c++  java
  • 1643 线段覆盖 3

    1643 线段覆盖 3

     

    时间限制: 2 s
    空间限制: 256000 KB
    题目等级 : 黄金 Gold
     
     
     
    题目描述 Description

    在一个数轴上有n条线段,现要选取其中k条线段使得这k条线段两两没有重合部分(端点可以重合),问最大的k为多少。

    输入描述 Input Description

    输入格式

    输入文件的第1行为一个正整数n,下面n行每行2个数字ai,bi,描述每条线段。

    输出描述 Output Description

    输出格式

      输出文件仅包括1个整数,为k的最大值

    样例输入 Sample Input

    3

    0 2

    2 4

    1 3

    样例输出 Sample Output

    2

    数据范围及提示 Data Size & Hint

    数据范围

    对于20%的数据,n≤10;

    对于50%的数据,n≤1000;

    对于70%的数据,n≤100000;

    对于100%的数据,n≤1000000,0≤ai<bi≤1000000。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 const int N=1000001;
     6 struct node{
     7     int q,h;
     8 }s[N];
     9 bool cmp(node a,node b)
    10 {
    11     return a.h<b.h;
    12 }
    13 int main()
    14 {
    15     int n;
    16     cin>>n;
    17     for(int i=1;i<=n;i++)
    18      {
    19          cin>>s[i].q>>s[i].h;
    20          if(s[i].q>s[i].h)swap(s[i].q,s[i].h);
    21      }
    22      sort(s+1,s+n+1,cmp);
    23      int ans=0;
    24      for(int i=1;i<=n-1;i++)
    25       {
    26           if(s[i].h>s[i+1].q)
    27            {
    28                ans++;
    29                s[i+1].h=s[i].h;
    30            }
    31       }
    32       cout<<n-ans;
    33 }
  • 相关阅读:
    react开发环境准备
    react介绍
    课程大纲
    Proving Equivalences HDU-2767 (tarjan缩点)
    tarjan求强连通分量 + 缩点 + 求割点割边
    树的重心(性质+模版)
    Educational Codeforces Round 93 (Rated for Div. 2)(A B C D)
    Friend-Graph HDU
    Codeforces Round #665 (Div. 2) (A B C D E)
    Matrix Again HDU
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/6773428.html
Copyright © 2011-2022 走看看