Advertisement
allia

сортировка критерии структуры

Nov 9th, 2020
1,160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct team
  8. {
  9.   int number;
  10.   int point;
  11.   int time;
  12. };
  13.  
  14. void merge_series (team *arr, int first, int last_1, int last_2, int *dubl, int *ind)
  15. {
  16.   int i = first;
  17.   int j = last_1+1;
  18.   int c = 0;
  19.  
  20.  for (c = first; c <= last_2; c++)
  21.   if (j > last_2)
  22.     dubl[c] = ind[i++];
  23.      else if (i > last_1)
  24.       dubl[c] = ind[j++];
  25.       else if (arr[ind[i]].point > arr[ind[j]].point)
  26.        dubl[c] = ind[i++];
  27.         else if (arr[ind[i]].point == arr[ind[j]].point)
  28.         {
  29.           if (arr[ind[i]].time <= arr[ind[j]].time)
  30.           dubl[c] = ind[i++];
  31.           else dubl[c] = ind[j++];
  32.         }
  33.           else dubl[c] = ind[j++];
  34. }
  35.  
  36. void sort (team *arr, int first, int last, int *dubl, int *ind)
  37. {
  38.   int middle = (first + last)/2;
  39.  
  40.   if (first < middle)
  41.    sort (arr, first, middle, dubl, ind);
  42.   if (middle + 1 < last)
  43.    sort (arr, middle+1, last, dubl, ind);
  44.  
  45.   merge_series (arr, first, middle, last, dubl, ind);
  46.  
  47.  
  48.   for (int i = first; i <= last; i++)
  49.      ind[i] = dubl[i];
  50. }
  51.  
  52. int main ()
  53. {
  54.   int n;
  55.   int *ind, *dubl;
  56.   cin >> n;
  57.  
  58.   team *arr = new team[n];
  59.   ind = new int [n];
  60.   dubl = new int [n];
  61.  
  62.   for (int i=0; i<n; i++)
  63.   {
  64.    arr[i].number = i+1;
  65.    ind[i] = i;
  66.    cin >> arr[i].point;
  67.    cin >> arr[i].time;
  68.   }
  69.  
  70.  sort (arr, 0, n-1, dubl, ind);
  71.    
  72.   for (int i=0; i<n; i++)
  73.   cout << ind[i] + 1 << " ";
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement