Submission
Status:
[PPPPP][PPPPP]
Score: 100
User: Pera
Problemset: จุดแวะพัก
Language: cpp
Time: 0.010 second
Submitted On: 2025-03-17 21:15:38
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<pair<string, int>> visitedK; // name, stops before K
for (int i = 0; i < n; i++) {
string name;
cin >> name;
vector<int> stops;
int stop;
while (cin >> stop) {
stops.push_back(stop);
if (cin.peek() == '\n' || cin.peek() == EOF) break;
}
auto it = find(stops.begin(), stops.end(), k);
if (it != stops.end()) {
int stopsBefore = distance(stops.begin(), it);
visitedK.push_back({name, stopsBefore});
}
}
// Sort by stops before K (ascending) and then by name
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;
});
// Print result
if (visitedK.empty()) {
cout << "-1" << endl;
} else {
for (int i = 0; i < min(3, (int)visitedK.size()); i++) {
cout << visitedK[i].first;
if (i < min(3, (int)visitedK.size()) - 1) cout << " ";
}
cout << endl;
}
return 0;
}