Advertisement
Guest User

Untitled

a guest
Oct 9th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct runner
  9. {
  10. int place;
  11. string name;
  12. string country;
  13. };
  14.  
  15. void mergeSort(vector<runner>& a, int start, int end)
  16. {
  17. if (start + 1 >= end)
  18. {
  19. return;
  20. }
  21. mergeSort(a, start, start + (end - start) / 2);
  22. mergeSort(a, start + (end - start) / 2, end);
  23.  
  24. vector<runner> b;
  25. int b1 = start;
  26. int e1 = start + (end - start) / 2;
  27.  
  28. int b2 = start + (end - start) / 2;
  29. int e2 = end;
  30. while (b.size() < end - start)
  31. {
  32. if (b1 == e1 || ((b2 < e2) &&(a[b2].country < a[b1].country)))
  33. {
  34. b.push_back(a[b2]);
  35. b2++;
  36. }
  37. else
  38. {
  39. b.push_back(a[b1]);
  40. b1++;
  41. }
  42. }
  43. for (int i = start; i < end; i++)
  44. {
  45. a[i] = b[i-start];
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. ifstream input("race.in");
  52. ofstream output("race.out");
  53.  
  54. vector<runner> a;
  55.  
  56. int n = 0;
  57. input >> n;
  58.  
  59. a.resize(n);
  60.  
  61. string inp_country;
  62. string inp_runner;
  63.  
  64. for (int i = 0; i < n; ++i)
  65. {
  66. input >> inp_country >> inp_runner;
  67. a[i].name = inp_runner;
  68. a[i].place = i+1;
  69. a[i].country = inp_country;
  70. }
  71.  
  72. mergeSort(a,0,n);
  73.  
  74. string curr_country = "";
  75.  
  76. for (int i = 0; i < n; i++)
  77. {
  78. if (curr_country != a[i].country)
  79. {
  80. curr_country = a[i].country;
  81. cout << "=== " << a[i].country << " ===" << endl;
  82. }
  83. cout << a[i].name << endl;
  84. }
  85. input.close();
  86. output.close();
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement