loulett

HW3_requests

Dec 13th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cassert>
  4.  
  5. struct request
  6. {
  7.     int start;
  8.     int finish;
  9. };
  10.  
  11. void swap(request &a, request &b)
  12. {
  13.     request t = a;
  14.     a = b;
  15.     b = t;
  16. }
  17.  
  18. int partition(request* a, int l, int r)
  19. {
  20.     int i = l - 1;
  21.     for (int j = l; j < r - 1; ++j)
  22.     {
  23.         if (a[j].finish <= a[r - 1].finish)
  24.         {
  25.             i++;
  26.             swap(a[i], a[j]);
  27.         }
  28.     }
  29.     swap(a[i + 1], a[r - 1]);
  30.     return i + 1;
  31. }
  32.  
  33. void qsort(request* a, int l, int r)
  34. {
  35.     if (l >= r)
  36.         return;
  37.     int q = partition(a, l, r);
  38.     qsort(a, l, q);
  39.     qsort(a, q + 1, r);
  40. }
  41.  
  42. int main()
  43. {
  44.     int n = 0, j = 0;
  45.     scanf("%d", &n);
  46.     request* arr = (request*)malloc(n*sizeof(request));
  47.     request t;
  48.     for (int i = 0; i < n; i++)
  49.     {
  50.         scanf("%d", &arr[i].start);
  51.         scanf("%d", &arr[i].finish);
  52.     }
  53.  
  54.     qsort(arr, 0, n);
  55.     t = arr[0];
  56.     for (int i = 1; i < n; i++)
  57.     {
  58.         if (arr[i].start > t.finish)
  59.         {
  60.             printf("%d %d\n", t.start, t.finish);
  61.             t = arr[i];
  62.         }
  63.     }
  64.     printf("%d %d\n", t.start, t.finish);
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment