A primary school recently received a sponsorship and decided to allocate a portion of it as scholarships for the top 5 students with excellent academic performance. At the end of the term, each student has scores for 3 subjects: Chinese, Mathematics, and English. They are first sorted in descending order based on their total scores. If two students have the same total score, they are further sorted in descending order based on their Chinese scores. If two students have the same total score and the same Chinese score, then the student with the lower student ID is ranked higher. This way, the ranking of each student is uniquely determined.
Task: First, calculate the total scores based on the input scores for the 3 subjects. Then, sort the students according to the rules described above. Finally, output the student ID and total score of the top 5 students in order of ranking. Note that among the top 5 students, each student's scholarship is different, so you must strictly follow the above rules for sorting. For example, in a correct answer, if the output for the first two lines (each line outputting two numbers: student ID, total score) is:
7 279
5 279
These two lines of data mean that the student with the highest total score is student number 7, followed by student number 5. Both students have a total score of 279 (the total score is equal to the sum of the scores for Chinese, Mathematics, and English), but student number 7 has a slightly higher score in Chinese.
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
EOF inputs: each set of input contains N+1 lines: The first line is a positive integer N, indicating the number of students participating in the selection. The next N lines (from the second to N+1th lines) each contain 3 numbers separated by spaces. Each number is between 0 and 100. The three numbers on the j-th line represent the scores for Chinese, Mathematics, and English, respectively, for the student with ID j-1.The student IDs are numbered from 1 to N in the order of input. (Exactly one less than the line number of the input data). | Each set of output consists of 5 lines, each containing two positive integers separated by a space. They represent, in order, the student ID and total score of the top 5 students. |
6 90 67 80 87 66 91 78 89 91 88 99 77 67 89 64 78 89 98 8 80 89 89 88 98 78 90 67 80 87 66 91 78 89 91 88 99 77 67 89 64 78 89 98 | 6 265 4 264 3 258 2 244 1 237 8 265 2 264 6 264 1 258 5 258 |
Thought Process
Use a Vector<Pair<Pair, int>> to store the data and sort it using Sort. Sort the data sequentially by total score, Chinese score, and then student ID.
Because smaller student IDs should be ranked higher, when storing the student IDs, they should be multiplied by -1. This way, larger student IDs will be ranked lower.
Remember to multiply the student ID by -1 again when outputting.
Sample Code-ZeroJudge B158: Scholarship
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
while (cin >> N)
{
vector<pair<pair<int, int>, int>>v;
for (int i = 0; i<N; i++)
{
int a, b, c;
cin >> a >> b >> c;
int total = a+b+c;
pair<int, int>d;
d.first = total;
d.second = a;
pair<pair<int, int>, int>e;
e.first = d;
e.second = (i+1)*(-1);
v.push_back(e);
}
sort(v.begin(), v.end());
int size = int(v.size()-1);
for (int i = size; i>=size-4; i--)
{
cout << v[i].second*-1 << " " << v[i].first.first << "\n";
}
cout << "\n";
}
}
//ZeroJudge B158
//Dr. SeanXD