Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- typedef struct Intervals{
- int left;
- int right;
- }Interval;
- int compFunct(const void *a, const void *b){
- int ar = (*(Interval *)a).right;
- int br = (*(Interval *)b).right;
- int al = (*(Interval *)a).left;
- int bl = (*(Interval *)b).left;
- //return al-bl;
- if(ar == br){
- return bl-al;
- }else{
- return ar-br;
- }
- //return ar-br;
- }
- void vectorOfLatency(int **vector, Interval *Vector, int count){
- int aux_latency = 0;
- *vector = malloc(count * sizeof(int));
- for(int i = 0 ; i < count ; i++){
- aux_latency += Vector[i].left;
- (*vector)[i] = aux_latency - Vector[i].right;
- }
- }
- void readIntervals(Interval **vector, int *count){
- scanf("%d", count);
- *vector = malloc(*count * sizeof(Interval));
- for(int i = 0 ; i < *count ; i++)
- scanf("%d %d", &(*vector)[i].left, &(*vector)[i].right);
- }
- void printIntervals(Interval *vector, int count){
- for(int i = 0 ; i < count ; i++)
- printf("[%d, %d]\n", vector[i].left, vector[i].right);
- }
- int maxOfVector(int *vector, int count){
- int aux = INT_MIN;
- for(int i = 0 ; i < count ; i++)
- if(aux < vector[i])
- aux = vector[i];
- return aux;
- }
- int main(){
- Interval *vector;
- int *vector_latency, count, aux;
- readIntervals(&vector, &count);
- qsort(vector, count, sizeof(Interval), compFunct);
- vectorOfLatency(&vector_latency, vector, count);
- aux = maxOfVector(vector_latency, count);
- printIntervals(vector, count);
- printf("%d ", aux);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement