37052023-03-02 12:25:13DoraTalálkozáscsharpAccepted 55/55196ms35576 KiB
using System;
using System.Collections.Generic;   //listához kell
using System.Linq;

public class Program
{
    static void Main()
    {
        const int MAX = 100000;
        int n = int.Parse(Console.ReadLine());
        List<int> start = new List<int>(new int[MAX + 1]);
        List<int> stop = new List<int>(new int[MAX + 1]);

        for (int i = 0; i < n; i++)
        {
            string[] input = Console.ReadLine().Split();
            int x = int.Parse(input[0]);
            int y = int.Parse(input[1]);
            start[x]++;
            stop[y]++;
        }

        for (int i = 1; i <= MAX; i++)
        {
            start[i] += start[i - 1];
            stop[i] += stop[i - 1];
        }

        int bestl = -1, bestr = -1, half = (n + 1) / 2;
        int l = 1, r = 1;

        while (r <= MAX)
        {
            while (r <= MAX && (Meet(start, stop, l, r) < half))
            {
                r++;
            }
            if (r > MAX)
            {
                break;
            }
            while (l <= r && (Meet(start, stop, l, r) >= half))
            {
                l++;
            }
            if (bestl == -1 || bestr - bestl > r - l)
            {
                bestl = l;
                bestr = r;
            }
        }
        Console.WriteLine(bestr - bestl + 2);
        Console.WriteLine($"{bestl - 1} {bestr}");
        Console.ReadKey();
    }

    static int Meet(List<int> start, List<int> stop, int l, int r)
    {
        if (l > r)
        {
            return 0;
        }
        return start[l] - stop[l - 1] + start[r] - start[l];
    }
}
SubtaskSumTestVerdictTimeMemory
base55/55
1Accepted0/037ms22956 KiB
2Accepted0/0184ms31856 KiB
3Accepted2/237ms24296 KiB
4Accepted2/235ms24236 KiB
5Accepted2/235ms24360 KiB
6Accepted2/235ms24548 KiB
7Accepted2/237ms24820 KiB
8Accepted3/335ms25080 KiB
9Accepted3/335ms25048 KiB
10Accepted3/337ms25596 KiB
11Accepted3/345ms27600 KiB
12Accepted3/348ms28800 KiB
13Accepted3/350ms28856 KiB
14Accepted3/365ms31968 KiB
15Accepted3/367ms31964 KiB
16Accepted3/364ms31936 KiB
17Accepted3/364ms32116 KiB
18Accepted3/383ms34808 KiB
19Accepted3/3196ms35284 KiB
20Accepted3/3182ms35132 KiB
21Accepted3/3194ms35292 KiB
22Accepted3/3190ms35576 KiB