Vserosbuybuy

C5.

Mar 28th, 2021 (edited)
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. #include <math.h>
  5. #include <vector>
  6. #include <string>
  7. #include <sstream>
  8. #include <map>
  9. #include <set>
  10.  
  11. struct Applicant {
  12.     std::string name, surname;
  13.     unsigned int day, month, year, score;
  14.     std::vector<std::string> uni;
  15. };
  16.  
  17. struct ApplicantPred {
  18.     // defines whether to consider score while sorting or not
  19.     bool considerScore; // Represents a comparator state.
  20.  
  21.     ApplicantPred(bool consScore = false)
  22.         : considerScore(consScore) {} // By default we do NOT consider score, only
  23.  
  24.     bool operator() (const Applicant& lhv, const Applicant& rhv)
  25.         const // By definition, this method MUST be marked
  26.     {
  27.         if (considerScore) {
  28.             if (lhv.score != rhv.score) {
  29.                 return lhv.score > rhv.score;
  30.             }
  31.  
  32.             if (lhv.year != rhv.year) {
  33.                 return lhv.year < rhv.year;
  34.             }
  35.  
  36.             if (lhv.month != rhv.month) {
  37.                 return lhv.month < rhv.month;
  38.             }
  39.  
  40.             if (lhv.day != rhv.day) {
  41.                 return lhv.day < rhv.day;
  42.             }
  43.  
  44.             if (lhv.surname != rhv.surname) {
  45.                 return lhv.surname < rhv.surname;
  46.             }
  47.  
  48.             return lhv.name < rhv.name;
  49.         }
  50.         else {
  51.             if (lhv.surname != rhv.surname) {
  52.                 return lhv.surname < rhv.surname;
  53.             }
  54.  
  55.             if (lhv.name != rhv.name) {
  56.                 return lhv.name < rhv.name;
  57.             }
  58.  
  59.             if (lhv.year != rhv.year) {
  60.                 return lhv.year < rhv.year;
  61.             }
  62.  
  63.             if (lhv.month != rhv.month) {
  64.                 return lhv.month < rhv.month;
  65.             }
  66.  
  67.             return lhv.day < rhv.day;
  68.         }
  69.     }
  70. };
  71.  
  72. typedef std::map<std::string, unsigned int> UniCountMap;
  73. typedef std::map<std::string, std::set<Applicant, ApplicantPred>> UniStudentsMap;
  74. typedef std::vector<Applicant> ApplicantsVector;
  75.  
  76. void readUni(UniCountMap& mp) {
  77.     int n;
  78.     std::cin >> n;
  79.     std::string s;
  80.     for (int i = 0; i < n; ++i) {
  81.         std::cin >> s >> mp[s];
  82.     }
  83. }
  84.  
  85. void readStudents(ApplicantsVector& v) {
  86.     int cnt_st;
  87.     std::string s;
  88.  
  89.     std::cin >> cnt_st;
  90.     for (int i = 0; i < cnt_st; ++i) {
  91.         Applicant st;
  92.         std::cin >> st.name >> st.surname;
  93.         std::cin >> st.day >> st.month >> st.year;
  94.         std::cin >> st.score;
  95.         int cnt;
  96.         std::cin >> cnt;
  97.         for (int i = 0; i < cnt; ++i) {
  98.             std::cin >> s;
  99.             st.uni.push_back(s);
  100.         }
  101.         v.push_back(st);
  102.     }
  103. }
  104.  
  105. void sortStudents(ApplicantsVector& v) {
  106.     ApplicantPred pred(true);
  107.     std::sort(v.begin(), v.end(), pred);
  108. }
  109.  
  110. UniStudentsMap processStudents(UniCountMap& mp, ApplicantsVector& v) {
  111.     UniStudentsMap students;
  112.  
  113.     for (auto& [uni, cnt] : mp) {
  114.         students[uni] = {};
  115.     }
  116.  
  117.     for (auto& student : v) {
  118.         for (auto& uni : student.uni) {
  119.             if (students[uni].size() < mp[uni]) {
  120.                 students[uni].insert(student);
  121.                 break;
  122.             }
  123.         }
  124.     }
  125.     return students;
  126. }
  127.  
  128. void printStudents(UniStudentsMap& students) {
  129.     for (auto& [k, v] : students) {
  130.         std::cout << k << "\t";
  131.        
  132.         for (auto& x : v) {
  133.             std::cout << x.name << " " << x.surname << "\t";
  134.         }
  135.         std::cout << "\n";
  136.     }
  137. }
  138.  
  139. int main() {
  140.     UniCountMap mp;
  141.     ApplicantsVector v;
  142.     readUni(mp);
  143.     readStudents(v);
  144.     sortStudents(v);
  145.     UniStudentsMap res = processStudents(mp, v);
  146.     printStudents(res);
  147.  
  148.     return 0;
  149. }
Add Comment
Please, Sign In to add comment