Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <limits.h>
- typedef struct Intervals{
- int left;
- int right;
- }Interval;
- void read_intervals(Interval **vector, int *count, FILE *f){
- fscanf(f, "%d", count);
- *vector = malloc(*count * sizeof(Interval));
- for(int i = 0 ; i < *count ; i++){
- fscanf(f, "%d %d", &(*vector)[i].left, &(*vector)[i].right);
- }
- }
- int comp_funct(const void *a, const void *b){
- int al = (*(Interval *)a).right;
- int bl = (*(Interval *)b).right;
- return al-bl;
- }
- Interval interval_intersection(Interval a, Interval b){
- Interval aux;
- if(a.left >= b.left && a.left <= b.right && a.right >= b.left){
- aux.left = a.left;
- aux.right = b.right;
- }
- if(a.left <= b.left && b.left <= a.right && b.right >= a.right){
- aux.left = b.left;
- aux.right = a.right;
- }
- if(a.right < b.left){
- aux.left = 0;
- aux.right = 0;
- }
- if(a.left >= b.left && a.right <= b.right){
- aux.left = a.left;
- aux.right = a.right;
- }
- if(b.left >= a.left && b.right <= a.right){
- aux.left = b.left;
- aux.right = b.right;
- }
- return aux;
- }
- void print_intervals(Interval *vector, int count){
- for(int i = 0 ; i < count ; i++)
- printf("%d - %d \n", vector[i].left, vector[i].right);
- }
- int main(){
- Interval *vector, interval_aux;
- FILE *f;
- f = fopen("date.in", "r");
- int count, aux, writers_count = 1, max_writers_count = INT_MIN, aux_pref;
- read_intervals(&vector, &count, f);
- qsort(vector, count, sizeof(Interval), comp_funct);
- interval_aux = vector[0];
- for(int i = 1 ; i < count; i++){
- //printf("%d %d \n", interval_aux.left, interval_aux.right);
- interval_aux = interval_intersection(interval_aux, vector[i]);
- //printf("%d %d \n", interval_aux.left, interval_aux.right);
- if(interval_aux.left == 0 && interval_aux.right == 0){
- writers_count = 1;
- interval_aux = vector[i];
- i--;
- }else{
- writers_count++;
- aux = interval_aux.left;
- }
- if(writers_count > max_writers_count){
- max_writers_count = writers_count;
- aux_pref = aux;
- }
- }
- print_intervals(vector, count);
- //scanf("%d %d", &a.left, &a.right);
- //scanf("%d %d", &b.left, &b.right);
- //interval_ptr = interval_intersection(a,b);
- //printf("%d %d", interval_ptr.left, interval_ptr.right);
- printf("%d ", max_writers_count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement