Submission
Status:
[PPPPP][-SSSS]
Score: 50
User: Pera
Problemset: จุดแวะพัก
Language: cpp
Time: 0.012 second
Submitted On: 2025-03-24 22:36:02
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, target;
// no one visited -> -1
// first 3 people
// if same stops, consider name
cin >> n >> target;
vector<pair<string, int>> visitedK;
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(), target);
if (it != stops.end()) {
visitedK.push_back({name, distance(stops.begin(), it)});
}
}
sort(visitedK.begin(), visitedK.end(), [](pair<string, int>& a, pair<string, int>& b) {
if (a.second != b.second) return a.second < b.second;
else {
return a.first < b.first;
}
});
if (visitedK.empty()) {
cout << -1 << '\n';
} else {
int count{0};
for (pair<string, int> p : visitedK) {
if (count > 3) break;
cout << p.first << ' ';
count++;
}
cout << '\n';
}
}