Submission
Status:
[-SSSS][-SSSS]
Score: 0
User: Cmoss9
Problemset: จุดแวะพัก
Language: cpp
Time: 0.052 second
Submitted On: 2025-03-23 13:55:09
#include <bits/stdc++.h>
using namespace std;
int main () {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, k;
cin >> n >> k;
cin.ignore();
map<string, vector<int>> traveler;
vector<string> input(n);
for (int i = 0; i < n; i++) {
getline(cin, input[i]);
int pos = 0;
while (pos < input[i].size() && isspace(input[i][pos]))
pos++;
string name = "";
while (pos < input[i].size() && !isspace(input[i][pos])) {
name.push_back(input[i][pos]);
pos++;
}
while (pos < input[i].size()) {
while (pos < input[i].size() && isspace(input[i][pos]))
pos++;
if (pos >= input[i].size()) break;
int num = 0;
while (pos < input[i].size() && isdigit(input[i][pos])) {
num = num * 10 + (input[i][pos] - '0');
pos++;
}
traveler[name].push_back(num);
}
}
vector<pair<string,int>> stop(n, {"", 100001});
for (auto i : traveler) {
for (auto j : i.second) {
auto it = find(i.second.begin(), i.second.end(), k);
if (it != i.second.end()) {
stop.push_back({i.first, distance(i.second.begin(), it)});
}
}
}
sort(stop.begin(), stop.end(), [](pair<string,int> a, pair<string,int> b) {
if (a.second != b.second) return a.second < b.second;
return a.first < b.first;
});
int count = 0;
vector<string> outname(3);
for (auto i : stop) {
if (count == 3) {
break;
}
if (find(outname.begin(), outname.end(), i.first) == outname.end()) {
cout << i.first << '\n';
outname[count] = i.first;
count++;
}
}
}