Submission

Status:
[PPPPP][PPPPP]

Score: 100

User: kaka

Problemset: จุดแวะพัก

Language: cpp

Time: 0.019 second

Submitted On: 2025-03-25 00:13:32

#include <bits/stdc++.h>
using namespace std;
int main()
{

    int n, target;
    cin >> n >> target;
    cin.ignore();
    vector<pair<string, int>> vit;
    string s;
    string name;
    for(int i = 0; i < n; i++){
        cin >> name;
        cin.ignore();
        getline(cin, s);
        stringstream iss(s);
        int stop;
        vector<int> stops;
        while(iss >> stop){
            stops.push_back(stop);
        }
        auto it = find(stops.begin(), stops.end(), target);
        if(it != stops.end()){
            vit.push_back({name, distance(stops.begin(), it)});
        }
    }
    sort(vit.begin(), vit.end(), [](auto a, auto b){
        if(a.second != b.second) return a.second < b.second;
        return a.first < b.first;
    });
    if(vit.empty()){
        cout << -1 << endl;
        return 0;
    }
    else{
        for(int i = 0; i < min(3, int(vit.size())); i++){
            cout << vit[i].first << ' ';
        }
    }
}