153392025-02-18 16:22:17PKBLeghosszabb béke (75 pont)cpp17Elfogadva 75/7575ms1148 KiB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){

    int N, M;
    cin >> N >> M;
    vector<pair<int,int>> wars(M);
    for (int i = 0; i < M; i++){
        int a, b;
        cin >> a >> b;
        wars[i] = {a, b};
    }
    sort(wars.begin(), wars.end());


    vector<pair<int,int>> merged;
    int curL = wars[0].first, curR = wars[0].second;
    for (int i = 1; i < M; i++){
        int L = wars[i].first, R = wars[i].second;
        if(L <= curR + 1){
            curR = max(curR, R);
        } else {
            merged.push_back({curL, curR});
            curL = L;
            curR = R;
        }
    }
    merged.push_back({curL, curR});

    int bestLength = 0, bestStart = -1;

    if(merged[0].first > 1){
        int gapLength = merged[0].first - 1;
        bestLength = gapLength;
        bestStart = 1;
    }

    for (int i = 0; i < merged.size() - 1; i++){
        int gapStart = merged[i].second + 1;
        int gapEnd = merged[i+1].first - 1;
        if(gapStart <= gapEnd){
            int gapLength = gapEnd - gapStart + 1;
            if(gapLength > bestLength || (gapLength == bestLength && gapStart < bestStart)){
                bestLength = gapLength;
                bestStart = gapStart;
            }
        }
    }

    if(merged.back().second < N){
        int gapStart = merged.back().second + 1;
        int gapLength = N - merged.back().second;
        if(gapLength > bestLength || (gapLength == bestLength && gapStart < bestStart)){
            bestLength = gapLength;
            bestStart = gapStart;
        }
    }

    if(bestLength <= 0)
        cout << -1 << "\n";
    else
        cout << bestLength << " " << bestStart << "\n";

    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base75/75
1Elfogadva0/01ms508 KiB
2Elfogadva0/075ms1076 KiB
3Elfogadva3/31ms508 KiB
4Elfogadva3/31ms316 KiB
5Elfogadva3/31ms316 KiB
6Elfogadva3/31ms316 KiB
7Elfogadva3/31ms316 KiB
8Elfogadva4/41ms316 KiB
9Elfogadva4/41ms316 KiB
10Elfogadva4/41ms316 KiB
11Elfogadva4/44ms444 KiB
12Elfogadva4/46ms316 KiB
13Elfogadva4/44ms316 KiB
14Elfogadva4/44ms316 KiB
15Elfogadva4/46ms316 KiB
16Elfogadva4/47ms316 KiB
17Elfogadva4/47ms316 KiB
18Elfogadva4/48ms476 KiB
19Elfogadva4/468ms1076 KiB
20Elfogadva4/471ms1148 KiB
21Elfogadva4/468ms1116 KiB
22Elfogadva4/471ms1148 KiB