Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4.  
  5. typedef struct Intervals{
  6.     int left;
  7.     int right;
  8. }Interval;
  9.  
  10. int compFunct(const void *a, const void *b){
  11.     int ar = (*(Interval *)a).right;
  12.     int br = (*(Interval *)b).right;
  13.     int al = (*(Interval *)a).left;
  14.     int bl = (*(Interval *)b).left;
  15.     //return al-bl;
  16.     if(ar == br){
  17.         return bl-al;
  18.     }else{
  19.         return ar-br;
  20.     }
  21.     //return ar-br;
  22. }
  23.  
  24. void vectorOfLatency(int **vector, Interval *Vector, int count){
  25.     int aux_latency = 0;
  26.     *vector = malloc(count * sizeof(int));
  27.     for(int i = 0 ; i < count ; i++){
  28.         aux_latency += Vector[i].left;
  29.         (*vector)[i] = aux_latency - Vector[i].right;
  30.     }
  31. }
  32.  
  33. void readIntervals(Interval **vector, int *count){
  34.     scanf("%d", count);
  35.     *vector = malloc(*count * sizeof(Interval));
  36.     for(int i = 0 ; i < *count ; i++)
  37.         scanf("%d %d", &(*vector)[i].left, &(*vector)[i].right);
  38. }
  39.  
  40. void printIntervals(Interval *vector, int count){
  41.     for(int i = 0 ; i < count ; i++)
  42.         printf("[%d, %d]\n", vector[i].left, vector[i].right);
  43. }
  44.  
  45. int maxOfVector(int *vector, int count){
  46.     int aux = INT_MIN;
  47.     for(int i = 0 ; i < count ; i++)
  48.         if(aux < vector[i])
  49.             aux = vector[i];
  50.     return aux;
  51. }
  52.  
  53. int main(){
  54.     Interval *vector;
  55.     int *vector_latency, count, aux;
  56.     readIntervals(&vector, &count);
  57.     qsort(vector, count, sizeof(Interval), compFunct);
  58.     vectorOfLatency(&vector_latency, vector, count);
  59.     aux = maxOfVector(vector_latency, count);
  60.     printIntervals(vector, count);
  61.     printf("%d ", aux);
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement