144562025-01-10 20:58:36zedÁruszállítás üres szakaszaicpp17Wrong answer 0/50263ms4404 KiB
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> routes(m);
    for (int i=0;i<m;i++){
        cin >> routes[i].first >> routes[i].second;
    }
    sort(routes.begin(), routes.end());
//    for (int i=0;i<m;i++){
//        cout << routes[i].first << " -> " << routes[i].second << endl;
//    }
    vector<pair<int, int>> merged_routes(0);
    pair<int, int> merged_route = routes[0];

    if(m==1){
        int result;
        if(merged_route.first > 1 and merged_route.second <n){
            result = 2;
        }else if (merged_route.first > 1 or merged_route.second <n){
            result = 1;
        }else{
            result = 0;
        }
        cout << result;
    }else{
        for(int i=1;i<m;i++){
            pair<int, int> current_route = routes[i];

            // there are a few cases how two intervals can be positioned
            // relative to each other, some of the cases will not happen due
            // to the sorting
            // current.first < previous.first <- cannot happen, it is guaranteed
            // that current_route.first >= merged_route.first
            if(current_route.first > merged_route.second){// no overlap
                merged_routes.push_back(merged_route);
                merged_route = current_route;
                if(i==m-1){ // if we happen to have our last route as a new non-overlapping route, we need to add it separately
                    merged_routes.push_back(merged_route);
                }
            }else if(current_route.second > merged_route.second){ // we merge the two (extend the current interval)
                  merged_route.second = current_route.second;
            }// else do nothing because the merged route already fully contains the current one
            cout << current_route.first << " - " << current_route.second << "    " << merged_route.first << " " << merged_route.second << endl;
        }
    }
//    cout << "merged routes: \n";
//    for (auto r:merged_routes){
//        cout << r.first << " -> " << r.second << endl;
//    }

    // if we assume that the first merged route starts at 1 and the last merged
    // route ends at n, then the number of sections not covered by routes is
    // merged_routes.size() - 1. This needs to be incremented by one at each end if
    // the first condition is false respectively
    int result = merged_routes.size() -1;
    if(merged_routes[0].first > 1) result++;
    if(merged_routes[merged_routes.size()-1].second < n) result++;
    cout << result;
}
SubtaskSumTestVerdictTimeMemory
base0/50
1Wrong answer0/01ms316 KiB
2Wrong answer0/0256ms4404 KiB
3Runtime error0/21ms500 KiB
4Runtime error0/21ms316 KiB
5Wrong answer0/21ms316 KiB
6Wrong answer0/21ms316 KiB
7Wrong answer0/21ms316 KiB
8Wrong answer0/21ms316 KiB
9Wrong answer0/21ms316 KiB
10Wrong answer0/21ms316 KiB
11Wrong answer0/21ms316 KiB
12Wrong answer0/22ms508 KiB
13Wrong answer0/317ms580 KiB
14Wrong answer0/323ms568 KiB
15Wrong answer0/314ms564 KiB
16Wrong answer0/3217ms3636 KiB
17Wrong answer0/3229ms3636 KiB
18Wrong answer0/3263ms4148 KiB
19Wrong answer0/321ms564 KiB
20Wrong answer0/327ms1012 KiB
21Wrong answer0/3238ms3892 KiB
22Wrong answer0/3241ms4148 KiB