26622023-01-18 13:32:30kicsiboglarTalálkozáscpp11Wrong answer 52/5586ms15724 KiB
#include <iostream>
//#include <fstream>
#include <vector>
#include <deque>
#include <algorithm>
#include <climits>
#include <map>
#define ll long long 

using namespace std;
//ifstream cin ("input.in");
//ofstream cout ("output.out");

ll n,m,i,j,a,b,len=LLONG_MAX,start,nr,pos,mini=LLONG_MAX,curr;


struct element
{
    ll start=0,stop=0;
};


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin>>n;
    map<ll,element> x;
    for (i=1;i<=n;++i)
    {
        cin>>a>>b;
        if (b-a+1<len)
        {
            len=b-a+1;
            pos=a;
        }
        //len=min(len,b-a+1);
        x[a].start++;
        x[b].stop++;
    }
    if (n<=2)
    {
        cout<<len<<"\n"<<pos<<" "<<pos+len-1;
        return 0;
    }
    deque <ll> v;
    a=0;
    ll half=(n+1)/2;
    pos=1;
    for (auto &e:x)
    {
        v.push_back(e.first);
        a+=e.second.start;
        while (!v.empty()&&a>half)
        {
            if (a-x[v[0]].stop>=half)
            {
                a-=x[v[0]].stop;
                v.pop_front();
            }
            else break;
        }
        if (a>=half&&e.first-v[0]+1<mini)
        {
            mini=e.first-v[0]+1;
            pos=v[0];
        }
        while (a==half&&!v.empty())
        {
            if (x[v[0]].stop>0) break;
            else v.pop_front();
        }
        if (a>=half&&e.first-v[0]+1<mini)
        {
            mini=e.first-v[0]+1;
            pos=v[0];
        }
    }
    
    cout<<mini<<"\n"<<pos<<" "<<pos+mini-1;

}
SubtaskSumTestVerdictTimeMemory
base52/55
1Accepted0/03ms1828 KiB
2Accepted0/075ms13444 KiB
3Accepted2/22ms2228 KiB
4Accepted2/22ms2428 KiB
5Accepted2/22ms2660 KiB
6Accepted2/22ms2988 KiB
7Accepted2/22ms3072 KiB
8Accepted3/32ms3056 KiB
9Accepted3/32ms3144 KiB
10Accepted3/32ms3160 KiB
11Accepted3/37ms4724 KiB
12Accepted3/38ms5492 KiB
13Accepted3/39ms5708 KiB
14Accepted3/316ms7116 KiB
15Accepted3/317ms7176 KiB
16Accepted3/316ms7084 KiB
17Accepted3/316ms7232 KiB
18Accepted3/327ms10108 KiB
19Wrong answer0/386ms12468 KiB
20Accepted3/320ms4036 KiB
21Accepted3/375ms15424 KiB
22Accepted3/376ms15724 KiB