76562024-01-10 10:45:43CsongiA lehető legkevesebb átszállás (50 pont)cpp17Runtime error 38/50192ms63112 KiB
#include <iostream>
#include <vector>

using namespace std;

pair<int, int> elerheto_csucs(int v, const vector<vector<int>>& allomasok, const vector<pair<int, int>>& vonatok)
{
    int legujabb = 0, index = 0;
    
    for (int i : allomasok[v])
    {
        if (vonatok[i].second > index)
        {
            legujabb = i;
            index = vonatok[i].second;
        }
    }
    
    return {legujabb, index};
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    
    vector<pair<int, int>> vonatok(n);
    vector<vector<int>> allomasok(m+1);
    
    bool elso = false, utolso = false;
    
    for (int i = 0; i < n; i++)
    {
        int kezdo, veg;
        cin >> kezdo >> veg;
        vonatok[i] = {kezdo, veg};
        
        if (kezdo == 1)
            elso = true;
        if (veg == m)
            utolso = true;
        
        for (int j = kezdo; j <= veg; j++)
        {
            allomasok[j].push_back(i);
        }
    }
    
    if (!elso || !utolso)
    {
        cout << "-1";
    }
    else
    {
        int jelenlegi = 1;
        vector<int> bejaras;
        
        while (jelenlegi < m)
        {
            auto [legujabb, ujIndex] = elerheto_csucs(jelenlegi, allomasok, vonatok);
            if (ujIndex <= jelenlegi) {
                cout << "-1";  // vegtelen loop?
                return 0;
            }
            jelenlegi = ujIndex;
            bejaras.push_back(legujabb);
        }
        
        cout << bejaras.size() - 1 << '\n';
        
        for (int i : bejaras)
        {
            cout << i + 1 << ' ';
        }
    }   
}
SubtaskSumTestVerdictTimeMemory
base38/50
1Accepted0/03ms1704 KiB
2Accepted0/057ms36664 KiB
3Accepted1/13ms2136 KiB
4Accepted1/13ms2436 KiB
5Accepted2/23ms2428 KiB
6Accepted2/23ms2376 KiB
7Accepted2/27ms5940 KiB
8Accepted2/28ms7156 KiB
9Accepted2/29ms8548 KiB
10Accepted2/214ms11468 KiB
11Accepted2/224ms16876 KiB
12Accepted2/228ms20792 KiB
13Accepted2/28ms7148 KiB
14Accepted2/217ms11624 KiB
15Accepted2/221ms15412 KiB
16Accepted2/229ms20352 KiB
17Accepted2/245ms29712 KiB
18Accepted2/246ms31544 KiB
19Accepted2/250ms34144 KiB
20Accepted2/254ms35760 KiB
21Accepted2/252ms37776 KiB
22Accepted2/252ms38376 KiB
23Runtime error0/2112ms63112 KiB
24Runtime error0/2128ms63080 KiB
25Runtime error0/2160ms62916 KiB
26Runtime error0/2144ms62680 KiB
27Runtime error0/2182ms62652 KiB
28Runtime error0/2192ms62640 KiB