Submission

Status:
[PPPPP][PPPPP]

Score: 100

User: kaka

Problemset: จุดแวะพัก

Language: cpp

Time: 0.007 second

Submitted On: 2025-03-24 23:38:45

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);

    int n, target; 
    cin >> n >> target;
    vector<pair<string, int>> visitedK;
    
    cin.ignore(); 

    for (int i = 0; i < n; i++) {
        string name;
        cin >> name;

        string line;
        getline(cin, line);
        istringstream iss(line);

        vector<int> stops;
        int stop;
        while (iss >> stop) {
            stops.push_back(stop);
        }

        auto it = find(stops.begin(), stops.end(), target);
        if (it != stops.end()) {
            visitedK.push_back({name, distance(stops.begin(), it)});
        }
    }

    sort(visitedK.begin(), visitedK.end(), [](const pair<string, int>& a, const pair<string, int>& b) {
        if (a.second != b.second) return a.second < b.second;
        return a.first < b.first;
    });

    if (visitedK.empty()) {
        cout << -1 << '\n';
    } else {
        for (int i = 0; i < min(3, (int)visitedK.size()); i++) {
            cout << visitedK[i].first << ' ';
        }
        cout << '\n';
    }
}