Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define max(a, b) ( (a) > (b) ? (a) : (b) )
- void qs(int *arr, int first, int last);
- int main(){
- FILE *input = fopen("linear.in", "r"), *output = fopen("linear.out", "w");
- int n, m;
- int *stack, *times;
- fscanf(input, "%d\n", &n);
- stack = (int*) malloc(n * sizeof (int));
- times = (int*) malloc(n * sizeof (int));
- int p = -1, s = -1;
- for (int i = 0; i < n; i++){
- int coord, type;
- fscanf(input, "%d %d\n", &coord, &type);
- if (type == 1) {
- p++;
- stack[p] = coord;
- }
- else if (p >= 0) {
- s++;
- times[s] = (int) fabs(coord - stack[p]);
- p--;
- }
- }
- qs(times, 0, s);
- int k = n;
- int j = 0;
- fscanf(input, "%d\n", &m);
- for (int i = 0; i < m; i++){
- int time;
- fscanf(input, "%d", &time);
- while (j <= s && times[j] <= 2 * time){
- k = k - 2;
- j++;
- }
- fprintf(output, "%d\n", k);
- }
- free(times);
- free(stack);
- fclose(input);
- fclose(output);
- return 0;
- }
- void qs(int *arr, int first, int last){
- int i = first, j = last;
- int x = arr[(first + last) / 2];
- while (i < j){
- while (arr[i] < x) i++;
- while (arr[j] > x) j--;
- if (i <= j) {
- int t = arr[j];
- arr[j] = arr[i];
- arr[i] = t;
- i++;
- j--;
- }
- }
- if (first < j)
- qs(arr, first, j);
- if (i < first)
- qs(arr, i, last);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement