fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <fstream>
  6. #include <map>
  7. using namespace std;
  8.  
  9. struct STUDENT {
  10. string name;
  11. double p1, p2, avg;
  12. vector<int> options;
  13. };
  14.  
  15. STUDENT getInfor (string str) {
  16. STUDENT a;
  17. vector <int> commaPos;
  18. for (int i = 0; i < str.length(); i++) {
  19. if (str[i] == ',') commaPos.push_back(i);
  20. }
  21. commaPos.push_back(str.length());
  22. int i = 0;
  23. a.name = str.substr(1, commaPos[i] - 2);
  24. a.p1 = stod(str.substr(commaPos[i] + 1, commaPos[i + 1] - commaPos[i] - 1));
  25. i++;
  26. a.p2 = stod(str.substr(commaPos[i] + 1, commaPos[i + 1] - commaPos[i] - 1));
  27. i++;
  28. a.avg = (a.p1 + a.p2) / 2;
  29. while (commaPos[i] != str.length()) {
  30. (a.options).push_back(stoi(str.substr(commaPos[i] + 1, commaPos[i + 1] - commaPos[i] - 1)));
  31. i++;
  32. }
  33. return a;
  34. }
  35.  
  36. bool higher (STUDENT a, STUDENT b) {
  37. if (a.avg > b.avg) return true;
  38. if (a.avg == b.avg) {
  39. if (a.p1 > b.p1) return true;
  40. else if (a.p1 == b.p1) {
  41. if (a.p2 > b.p2) return true;
  42. }
  43. }
  44. return false;
  45. }
  46.  
  47. bool lower (STUDENT a, STUDENT b) {
  48. if (a.avg < b.avg) return true;
  49. if (a.avg == b.avg) {
  50. if (a.p1 < b.p1) return true;
  51. else if (a.p1 == b.p1) {
  52. if (a.p2 < b.p2) return true;
  53. }
  54. }
  55. return false;
  56. }
  57.  
  58. int partition (vector<STUDENT>& a, int l, int r) {
  59. STUDENT p = a[l];
  60. int i = l, j = r + 1;
  61. while (true) {
  62. do i++; while (higher(a[i], p) && i <= r);
  63. do j--; while (lower(a[j], p));
  64. if (i >= j) break;
  65. swap(a[i], a[j]);
  66. }
  67. swap(a[l], a[j]);
  68. return j;
  69. }
  70.  
  71. void quickSort (vector<STUDENT>& a, int l, int r) {
  72. if (l < r) {
  73. int s = partition(a, l, r);
  74. quickSort(a, l, s - 1);
  75. quickSort(a, s + 1, r);
  76. }
  77. }
  78.  
  79. void getList (vector<vector<STUDENT> >& finalList, vector<STUDENT> listOfStudents, vector<int> slots) {
  80. for (auto x : listOfStudents) {
  81. bool ac = false;
  82. for (auto op : x.options) {
  83. if (finalList[op].size() < slots[op] || ((finalList[op].back()).avg == x.avg && (finalList[op].back()).p1 == x.p1 && (finalList[op].back()).p2 == x.p2)) {
  84. ac = true;
  85. finalList[op].push_back(x);
  86. break;
  87. }
  88. }
  89. if (ac == false) {
  90. finalList[0].push_back(x);
  91. }
  92. }
  93. }
  94.  
  95.  
  96. int main () {
  97. int m;
  98. cout << "Enter m: ";
  99. cin >> m;
  100. cout << "Pairs option maximum number of admitted candidates, separated by blanks: " << endl;
  101. vector<int> slots(m + 1);
  102. for (int i = 0; i < m; i++) {
  103. int a, b;
  104. cin >> a >> b;
  105. slots[a] = b;
  106. }
  107. cin.ignore();
  108. cout << "Note: Ctrl + D on Mac/ Linux or Ctrl + Z on Windows to stop." << endl;
  109. vector<STUDENT> listOfStudents;
  110. string data;
  111. while(getline(cin, data)) {
  112. STUDENT tmp = getInfor(data);
  113. listOfStudents.push_back(tmp);
  114. }
  115.  
  116. vector<vector<STUDENT>> listByOptions(m+1);
  117.  
  118. quickSort(listOfStudents, 0, listOfStudents.size() - 1);
  119.  
  120. getList(listByOptions, listOfStudents, slots);
  121.  
  122. for (int i = 1; i <= m; i++) {
  123. if (listByOptions[i].size() != 0) {
  124. cout << "Successful candidates for option " << i << endl;
  125. }
  126. for (int j = 0; j < listByOptions[i].size(); j++) {
  127. cout << j + 1 << ". " << listByOptions[i][j].name << " " << fixed << setprecision(2) << listByOptions[i][j].avg << endl;
  128. }
  129. }
  130. if (listByOptions[0].size() != 0) {
  131. cout << "Unsuccessful candidates" << endl;
  132. for (int j = 0; j < listByOptions[0].size(); j++) {
  133. cout << j + 1 << ". " << listByOptions[0][j].name << " " << fixed << setprecision(2) << listByOptions[0][j].avg << endl;
  134. }
  135. }
  136.  
  137. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Enter m: Pairs option maximum number of admitted candidates, separated by blanks: 
Note: Ctrl + D on Mac/ Linux or Ctrl + Z on Windows to stop.