|
| 1 | +#include <iostream> |
| 2 | +#include <algorithm> |
| 3 | +#define N 1000 |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +struct Student { |
| 7 | + int id; |
| 8 | + char name[N]; |
| 9 | + char surname[N]; |
| 10 | + int subject1; |
| 11 | + int subject2; |
| 12 | + int subject3; |
| 13 | + int subjectSelected; |
| 14 | + bool specialCase; |
| 15 | +}; |
| 16 | + |
| 17 | +int total(Student x) { |
| 18 | + return x.subject1 + x.subject2 + x.subject3 + x.subjectSelected; |
| 19 | +} |
| 20 | + |
| 21 | +int total(Student *x) { |
| 22 | + return x->subject1 + x->subject2 + x->subject3 + x->subjectSelected; |
| 23 | +} |
| 24 | + |
| 25 | +bool compare(Student a, Student b) { |
| 26 | + if (a.specialCase) return false; |
| 27 | + int total_a = total(a); |
| 28 | + int total_b = total(b); |
| 29 | + |
| 30 | + if (total_a != total_b) return total_a > total_b; |
| 31 | + return a.subjectSelected > b.subjectSelected; |
| 32 | +} |
| 33 | + |
| 34 | +bool comparePtr(Student *a, Student *b) { |
| 35 | + if (a->specialCase) return false; |
| 36 | + int total_a = total(a); |
| 37 | + int total_b = total(b); |
| 38 | + |
| 39 | + if (total_a != total_b) return total_a > total_b; |
| 40 | + return a->subjectSelected > b->subjectSelected; |
| 41 | +} |
| 42 | + |
| 43 | +int main() { |
| 44 | + int n, m; |
| 45 | + cin >> n >> m; |
| 46 | + Student students[n]; |
| 47 | + Student *grantedStudents[m]; |
| 48 | + char specialCase[N]; |
| 49 | + |
| 50 | + for (int i = 0; i < n; ++i) { |
| 51 | + cin >> students[i].id |
| 52 | + >> students[i].name |
| 53 | + >> students[i].surname |
| 54 | + >> students[i].subject1 |
| 55 | + >> students[i].subject2 |
| 56 | + >> students[i].subject3 |
| 57 | + >> students[i].subjectSelected |
| 58 | + >> specialCase; |
| 59 | + students[i].specialCase = specialCase[0] == 'Y'; // Yes, not a full check |
| 60 | + } |
| 61 | + |
| 62 | + sort(students, students + n, compare); |
| 63 | + |
| 64 | + int j = 0; |
| 65 | + for (int i = 0; i < n && j < m; ++i) { |
| 66 | + if (students[i].specialCase) { |
| 67 | + grantedStudents[j] = students + i; |
| 68 | + j++; |
| 69 | + } |
| 70 | + } |
| 71 | + for (int i = 0, l = m - j; i < l; ++i) { |
| 72 | + grantedStudents[j] = students + i; |
| 73 | + j++; |
| 74 | + } |
| 75 | + |
| 76 | + sort(grantedStudents, grantedStudents + m, comparePtr); |
| 77 | + |
| 78 | + for (int i = 0; i < j; ++i) { |
| 79 | + cout << grantedStudents[i]->id << ' ' |
| 80 | + << grantedStudents[i]->name << ' ' |
| 81 | + << grantedStudents[i]->surname << ' ' |
| 82 | + << total(grantedStudents[i]) << endl; |
| 83 | + } |
| 84 | + |
| 85 | + return 0; // FIXME ??? |
| 86 | +} |
0 commit comments