Submission
Status:
[PPPPP][PPPPP]
Score: 100
User: nongbilly
Problemset: จุดแวะพัก
Language: cpp
Time: 0.018 second
Submitted On: 2025-03-24 21:19:53
#include <bits/stdc++.h>
using namespace std;
vector<int> rest(string r){
vector<int> ans;
stringstream ss(r);
int num;
while (ss >> num) {
ans.push_back(num);
}
return ans;
}
int visit(vector<int> &r, int k){
for (int i = 0; i < r.size(); i++) {
if (r[i] == k) return i + 1;
}
return -1;
}
int main(){
int n, k;
cin >> n >> k;
cin.ignore();
vector<pair<string, int>> tourist;
for(int i = 0; i < n; i++){
string name;
cin >> name;
cin.ignore();
string r;
getline(cin, r);
vector<int> resting = rest(r);
int v = visit(resting, k);
if(v != -1) tourist.push_back({name, v});
}
sort(tourist.begin(), tourist.end(), [](auto &a, auto &b){
if(a.second == b.second) return a.first < b.first;
return a.second < b.second;
});
if(tourist.empty()){
cout << -1;
return 0;
}
for (int i = 0; i < min(3, (int)tourist.size()); i++) {
if(i > 0) cout << " ";
cout << tourist[i].first;
}
return 0;
}