当前位置 : 主页 > 编程语言 > c语言 >

XI Samara Regional Intercollegiate Programming Contest Problem C. Third-Party Software

来源:互联网 收集:自由互联 发布时间:2023-09-06
Pavel is developing a game. To do that, he needs functions available in a third-party library too famous to be called. It is known that the function i first appeared in version ai and existed until version bi , and starting from the version

Pavel is developing a game. To do that, he needs functions available in a third-party library too famous
to be called. It is known that the function i first appeared in version ai and existed until version bi
, and
starting from the version bi + 1, it is absent in this library.
The library is not free and Pavel needs all the functions. Which minimal number of versions he need to
purchase to be able to use all the functions?
Input
The first line contains a single integer n (1 ≤ n ≤ 200000) — the number of the functions.
Each of the next n lines contains two integers ai and bi (1 ≤ ai ≤ bi ≤ 109
) — the interval of library
versions where function i was available.
Output
In the first line output a single integer k — the minimal number of library versions need to be purchased
to unlock all functions.
In the second line output k distinct integers — the numbers of versions need to be purchased.
If there are several possible answers, output any of them.
Example
standard input standard output
5
2 4
1 3
2 3
3 6
4 5
2
3 4

题意:给定若干区间,选取尽量少的点使得所有的区间都会被计算在内;
思路:
贪心:选取最少自然想到贪心,将所有区间按照右端点排序,右端点相同按照左端点升序排序

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 500005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-7
#define pll pair<ll,ll>



ll quickpow(ll a, ll b) {
    ll ans = 1;
    a = a % mod;
    while (b > 0) {
        if (b % 2)ans = ans * a;
        b = b / 2;
        a = a * a;
    }
    return ans;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a%b);
}

struct Nod {
    int id, l, r;
    bool operator < (const Nod &rhs)const {
        if (r != rhs.r)return r < rhs.r;
        else {
            return l < rhs.l;
        }
    }
}node[maxn];

int ans[maxn];
int main()
{
    ios::sync_with_stdio(false);
    int i, j;
    int n;
    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> node[i].l >> node[i].r;
    }
    int tot = 0;
    sort(node, node + n);
    ans[++tot] = node[0].r;
    for (i = 0; i < n; i++) {
        if (node[i].l <= ans[tot])continue;
        else {
            tot++;
            ans[tot] = node[i].r;
        }
    }
    cout << tot << endl;
    for (i = 1; i <= tot; i++) {
        cout << ans[i] << ' ';
    }
}
【文章原创作者:阿里云代理 http://www.558idc.com/aliyun.html 网络转载请说明出处】
网友评论