144562025-01-10 20:58:36zedÁruszállítás üres szakaszaicpp17Hibás válasz 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;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base0/50
1Hibás válasz0/01ms316 KiB
2Hibás válasz0/0256ms4404 KiB
3Futási hiba0/21ms500 KiB
4Futási hiba0/21ms316 KiB
5Hibás válasz0/21ms316 KiB
6Hibás válasz0/21ms316 KiB
7Hibás válasz0/21ms316 KiB
8Hibás válasz0/21ms316 KiB
9Hibás válasz0/21ms316 KiB
10Hibás válasz0/21ms316 KiB
11Hibás válasz0/21ms316 KiB
12Hibás válasz0/22ms508 KiB
13Hibás válasz0/317ms580 KiB
14Hibás válasz0/323ms568 KiB
15Hibás válasz0/314ms564 KiB
16Hibás válasz0/3217ms3636 KiB
17Hibás válasz0/3229ms3636 KiB
18Hibás válasz0/3263ms4148 KiB
19Hibás válasz0/321ms564 KiB
20Hibás válasz0/327ms1012 KiB
21Hibás válasz0/3238ms3892 KiB
22Hibás válasz0/3241ms4148 KiB