zoukankan      html  css  js  c++  java
  • codevs 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;

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    pair<int,int>p[1000001];
    typedef pair<int, int> pi;
    
    bool cmp(pi a,pi b)
    {
        if(a.second<b.second)return 1;
        else return 0;
        
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        int x;
        int a,b;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&a,&b);
            if(a>b)swap(a,b);
            p[i].first=a;p[i].second=b;
        }
        sort(p+1,p+n+1,cmp);
        int maxn=-1000001,ans=0;
        for(int i=1;i<=n;i++)
        {
            if(p[i].first>=maxn)
            {    
                ans++;maxn=p[i].second;
            }
            
        }
        cout<<ans;
    }
    /*10
    1 10
    9 20
    19 30
    29 40
    39 50
    49 60
    59 70
    69 80
    79 90
    89 100*/

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

  • 相关阅读:
    rest_framework规范
    跨域问题
    网站部署(二)
    服务器更改密码后,git不能连接问题
    网站部署(一)
    Ajax
    AJAX基本使用
    Java之JNDI详解
    IntelliJ IDEA(2018.3.3) 的介绍、安装、破解、配置与使用
    数据库修改密码风险高,如何保证业务持续,这几种密码双活方案可以参考
  • 原文地址:https://www.cnblogs.com/sssy/p/7061786.html
Copyright © 2011-2022 走看看