1324:整数区间

【题目描述】

请编程完成以下任务:

1.从文件中读取闭区间的个数及它们的描述;

2.找到一个含元素个数最少的集合,使得对于每一个区间,都至少有一个整数属于该集合,输出该集合的元素个数。

【输入】

首行包括区间的数目n,1≤n≤10000,接下来的n行,每行包括两个整数a,b,被一空格隔开,0≤a≤b≤10000,它们是某一个区间的开始值和结束值。

【输出】

第一行集合元素的个数,对于每一个区间都至少有一个整数属于该区间,且集合所包含元素数目最少。

【输入样例】

4
3 6
2 4
0 2
4 7

【输出样例】

2

该集合为:2,6

// Created on 2020/2/14

/*#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <climits>*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int idata=100000+5;
int n,m;
int thebegin[idata];
int thend[idata];
int cnt,flag;
int minn=INT_MAX,maxx=-1;
int sum;
bool judge;

pair<int ,int>thep[idata];

int main()
{
    int i,j;

    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>thebegin[i]>>thend[i];
        thep[i].first=thebegin[i];
        thep[i].second=thend[i];
    }

    sort(thep+1,thep+1+n);

    for(i=1;i<=n;i++)
    {
        if(maxx>=thep[i].first)
            continue;
        cnt++;
        maxx=thep[i].second;
    }

    cout<<cnt<<endl;
    return 0;
}
发布了189 篇原创文章 · 获赞 8 · 访问量 7229

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/104307096