Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <limits.h>
  4.  
  5. typedef struct Intervals{
  6.     int left;
  7.     int right;
  8. }Interval;
  9.  
  10. void read_intervals(Interval **vector, int *count, FILE *f){
  11.     fscanf(f, "%d", count);
  12.     *vector = malloc(*count * sizeof(Interval));
  13.     for(int i = 0 ; i < *count ; i++){
  14.         fscanf(f, "%d %d", &(*vector)[i].left, &(*vector)[i].right);
  15.     }
  16. }
  17.  
  18. int comp_funct(const void *a, const void *b){
  19.     int al = (*(Interval *)a).right;
  20.     int bl = (*(Interval *)b).right;
  21.     return al-bl;
  22. }
  23.  
  24. Interval interval_intersection(Interval a, Interval b){
  25.     Interval aux;
  26.     if(a.left >= b.left && a.left <= b.right && a.right >= b.left){
  27.         aux.left = a.left;
  28.         aux.right = b.right;
  29.     }
  30.     if(a.left <= b.left && b.left <= a.right && b.right >= a.right){
  31.         aux.left = b.left;
  32.         aux.right = a.right;
  33.     }
  34.     if(a.right < b.left){
  35.         aux.left = 0;
  36.         aux.right = 0;
  37.     }
  38.     if(a.left >= b.left && a.right <= b.right){
  39.         aux.left = a.left;
  40.         aux.right = a.right;
  41.     }
  42.     if(b.left >= a.left && b.right <= a.right){
  43.         aux.left = b.left;
  44.         aux.right = b.right;
  45.     }
  46.     return aux;    
  47. }
  48.  
  49. void print_intervals(Interval *vector, int count){
  50.     for(int i = 0 ; i < count ; i++)
  51.         printf("%d - %d \n", vector[i].left, vector[i].right);
  52. }
  53.  
  54. int main(){
  55.     Interval *vector, interval_aux;
  56.     FILE *f;
  57.     f = fopen("date.in", "r");
  58.     int count, aux, writers_count = 1, max_writers_count = INT_MIN, aux_pref;
  59.     read_intervals(&vector, &count, f);
  60.     qsort(vector, count, sizeof(Interval), comp_funct);
  61.     interval_aux = vector[0];
  62.     for(int i = 1 ; i < count; i++){
  63.         //printf("%d %d \n", interval_aux.left, interval_aux.right);
  64.         interval_aux = interval_intersection(interval_aux, vector[i]);
  65.         //printf("%d %d \n", interval_aux.left, interval_aux.right);
  66.         if(interval_aux.left == 0 && interval_aux.right == 0){
  67.             writers_count = 1;
  68.             interval_aux = vector[i];
  69.             i--;
  70.         }else{
  71.             writers_count++;
  72.             aux = interval_aux.left;
  73.         }
  74.         if(writers_count > max_writers_count){
  75.             max_writers_count = writers_count;
  76.             aux_pref = aux;
  77.         }
  78.     }
  79.     print_intervals(vector, count);
  80.     //scanf("%d %d", &a.left, &a.right);
  81.     //scanf("%d %d", &b.left, &b.right);
  82.     //interval_ptr = interval_intersection(a,b);
  83.     //printf("%d %d", interval_ptr.left, interval_ptr.right);
  84.     printf("%d ", max_writers_count);
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement