Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <fstream>
- #include <math.h>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <map>
- #include <set>
- struct Applicant {
- std::string name, surname;
- unsigned int day, month, year, score;
- std::vector<std::string> uni;
- };
- struct ApplicantPred {
- // defines whether to consider score while sorting or not
- bool considerScore; // Represents a comparator state.
- ApplicantPred(bool consScore = false)
- : considerScore(consScore) {} // By default we do NOT consider score, only
- bool operator() (const Applicant& lhv, const Applicant& rhv)
- const // By definition, this method MUST be marked
- {
- if (considerScore) {
- if (lhv.score != rhv.score) {
- return lhv.score > rhv.score;
- }
- if (lhv.year != rhv.year) {
- return lhv.year < rhv.year;
- }
- if (lhv.month != rhv.month) {
- return lhv.month < rhv.month;
- }
- if (lhv.day != rhv.day) {
- return lhv.day < rhv.day;
- }
- if (lhv.surname != rhv.surname) {
- return lhv.surname < rhv.surname;
- }
- return lhv.name < rhv.name;
- }
- else {
- if (lhv.surname != rhv.surname) {
- return lhv.surname < rhv.surname;
- }
- if (lhv.name != rhv.name) {
- return lhv.name < rhv.name;
- }
- if (lhv.year != rhv.year) {
- return lhv.year < rhv.year;
- }
- if (lhv.month != rhv.month) {
- return lhv.month < rhv.month;
- }
- return lhv.day < rhv.day;
- }
- }
- };
- typedef std::map<std::string, unsigned int> UniCountMap;
- typedef std::map<std::string, std::set<Applicant, ApplicantPred>> UniStudentsMap;
- typedef std::vector<Applicant> ApplicantsVector;
- void readUni(UniCountMap& mp) {
- int n;
- std::cin >> n;
- std::string s;
- for (int i = 0; i < n; ++i) {
- std::cin >> s >> mp[s];
- }
- }
- void readStudents(ApplicantsVector& v) {
- int cnt_st;
- std::string s;
- std::cin >> cnt_st;
- for (int i = 0; i < cnt_st; ++i) {
- Applicant st;
- std::cin >> st.name >> st.surname;
- std::cin >> st.day >> st.month >> st.year;
- std::cin >> st.score;
- int cnt;
- std::cin >> cnt;
- for (int i = 0; i < cnt; ++i) {
- std::cin >> s;
- st.uni.push_back(s);
- }
- v.push_back(st);
- }
- }
- void sortStudents(ApplicantsVector& v) {
- ApplicantPred pred(true);
- std::sort(v.begin(), v.end(), pred);
- }
- UniStudentsMap processStudents(UniCountMap& mp, ApplicantsVector& v) {
- UniStudentsMap students;
- for (auto& [uni, cnt] : mp) {
- students[uni] = {};
- }
- for (auto& student : v) {
- for (auto& uni : student.uni) {
- if (students[uni].size() < mp[uni]) {
- students[uni].insert(student);
- break;
- }
- }
- }
- return students;
- }
- void printStudents(UniStudentsMap& students) {
- for (auto& [k, v] : students) {
- std::cout << k << "\t";
- for (auto& x : v) {
- std::cout << x.name << " " << x.surname << "\t";
- }
- std::cout << "\n";
- }
- }
- int main() {
- UniCountMap mp;
- ApplicantsVector v;
- readUni(mp);
- readStudents(v);
- sortStudents(v);
- UniStudentsMap res = processStudents(mp, v);
- printStudents(res);
- return 0;
- }
Add Comment
Please, Sign In to add comment