Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define max(a, b) ( (a) > (b) ? (a) : (b) )
  6.  
  7.  
  8. void qs(int *arr, int first, int last);
  9.  
  10.  
  11. int main(){
  12. FILE *input = fopen("linear.in", "r"), *output = fopen("linear.out", "w");
  13.  
  14. int n, m;
  15. int *stack, *times;
  16. fscanf(input, "%d\n", &n);
  17.  
  18. stack = (int*) malloc(n * sizeof (int));
  19. times = (int*) malloc(n * sizeof (int));
  20.  
  21. int p = -1, s = -1;
  22. for (int i = 0; i < n; i++){
  23. int coord, type;
  24. fscanf(input, "%d %d\n", &coord, &type);
  25. if (type == 1) {
  26. p++;
  27. stack[p] = coord;
  28. }
  29. else if (p >= 0) {
  30. s++;
  31. times[s] = (int) fabs(coord - stack[p]);
  32. p--;
  33. }
  34. }
  35.  
  36. qs(times, 0, s);
  37.  
  38. int k = n;
  39. int j = 0;
  40. fscanf(input, "%d\n", &m);
  41. for (int i = 0; i < m; i++){
  42. int time;
  43. fscanf(input, "%d", &time);
  44. while (j <= s && times[j] <= 2 * time){
  45. k = k - 2;
  46. j++;
  47. }
  48. fprintf(output, "%d\n", k);
  49. }
  50.  
  51. free(times);
  52. free(stack);
  53.  
  54. fclose(input);
  55. fclose(output);
  56.  
  57. return 0;
  58. }
  59.  
  60. void qs(int *arr, int first, int last){
  61. int i = first, j = last;
  62. int x = arr[(first + last) / 2];
  63. while (i < j){
  64. while (arr[i] < x) i++;
  65. while (arr[j] > x) j--;
  66. if (i <= j) {
  67. int t = arr[j];
  68. arr[j] = arr[i];
  69. arr[i] = t;
  70. i++;
  71. j--;
  72. }
  73. }
  74. if (first < j)
  75. qs(arr, first, j);
  76. if (i < first)
  77. qs(arr, i, last);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement