Submission

Status:
[P-SSS][PPP-S]

Score: 0

User: Pera

Problemset: จุดแวะพัก

Language: cpp

Time: 0.011 second

Submitted On: 2025-03-17 20:59:48

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

bool tuplesorter(tuple<string, vector<int>, int>& a, tuple<string, vector<int>, int>& b);

int main() {
    ios_base::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;

    vector<tuple<string, vector<int>, int>> stop(n);

    //Set the default distance to -1
    for (int i = 0; i < n; i++) {
        get<2>(stop[i]) = -1;
    }

    
    /*
    for (int i = 0; i < n; i++) {
        //cout << "Entered loop";
        string name;
        //cin >> stop[i].first;
        cin >> name;
        get<0>(stop[i]) = name;
        string s;
        while (cin.peek() >= '0' && cin.peek() <= '9') {
            cin >> s;
            get<1>(stop[i]).push_back(stoi(s));
            }
            }*/
    
    //ignore \n left by k
    cin.ignore();

    for (int i = 0; i < n; i++) {
        string line;
        getline(cin >> ws, line);

        istringstream ss(line);
        string name;
        ss >> name;
        get<0>(stop[i]) = name;

        int num;
        while (ss >> num) {
            get<1>(stop[i]).push_back(num);
        }
    }
    
    
           // Find if the people have k stop
    for (int i = 0; i < n; i++) {
        vector<int>::iterator it = find(get<1>(stop[i]).begin(), get<1>(stop[i]).end(), k);

        if (it != get<1>(stop[i]).end()) {
            get<2>(stop[i]) = distance(get<1>(stop[i]).begin(), it);
        }
    }

    sort(stop.begin(),stop.end(), tuplesorter);

    //Sort and print the output
    int count = 0;
    for (int i = 0; i < n; i++) {

        if (count >= 3) break;
        if (get<2>(stop[i]) != -1) {
            cout << get<0>(stop[i]) << ' ';
            count ++;
        }
    }

    cout << '\n';

}

bool tuplesorter(tuple<string, vector<int>, int>& a, tuple<string, vector<int>, int>& b) {
    if (get<2>(a) != get<2>(b)) {
        return get<2>(a) < get<2>(b);  // First, compare the distance
    } else {
        return get<0>(a) < get<0>(b);  // If distances are the same, compare the names
    }
}