Challenge FIND

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
1.0 kB
3
Indexable
Never
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("find.inp");
ofstream fout("find.out");

typedef struct
{
    int x, y;
} range;

range a[100002];

bool compare(range a, range b)
{
    return a.x < b.x;
}

vector<range> rangeList;

int main()
{
    int n;
    fin >> n;
    for (int i = 0; i < n; i++)
    {
        fin >> a[i].x >> a[i].y;
    }

    sort(a, a + n, compare);

    rangeList.push_back(a[0]);
    for (int i = 1; i < n; i++)
    {
        int currentIndex = rangeList.size() - 1;
        if (rangeList[currentIndex].y >= a[i].x)
        {
            rangeList[currentIndex].y = a[i].y;
        }
        else
        {
            rangeList.push_back(a[i]);
        }
    }

    int result = 0;
    for (int i = 0; i < rangeList.size(); i++)
    {
        result = max(result, rangeList[i].y - rangeList[i].x);
    }

    fout << result;

    fin.close();
    fout.close();
    return 0;
}