Hi 有兽焉
You could refer to this sample:
The following code uses pair to bind the registration number and results, and the vector is used as an array to save this information. The sort function is used to sort the vector, and the subsequent code outputs information such as score lines as required.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, M;
cin >> n >> M;
vector<pair<int, int>> scores; // store number and score
for (int i = 0; i < n; i++) {
int k, s;
cin >> k >> s;
scores.push_back(make_pair(k, s));
}
// sort
sort(scores.begin(), scores.end(), [](const pair<int, int>& a, const pair<int, int>& b) {
if (a.second == b.second) {
return a.first < b.first;
}
return a.second > b.second;
});
// calculate interview score
int interview_cutoff = scores[M * 150 / 100 - 1].second;
// output interview score and number of people
// int result = static_cast<int>(floor(M * 1.5));
int result = static_cast<int>(std::ceil(M * 1.5));
cout << interview_cutoff << " " << result << endl;
// output number and score
for (int i = 0; i < result; i++) {
cout << scores[i].first << " " << scores[i].second << endl;
}
return 0;
}
Result:
Best regards,
Elya
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.