|
1 | 1 | #include <stdio.h> |
2 | 2 | #include <stdlib.h> |
3 | 3 |
|
4 | | -int cmp(const void *a, const void *b) |
5 | | -{ |
6 | | - return (*(int*)a - *(int*)b); |
7 | | -} |
| 4 | +int cmp(const void *a, const void *b) { return (*(int*)a - *(int*)b); } |
8 | 5 | ////////////////////////////////////////////////////// |
9 | | -int buscaBinaria(int *vetor, int n, int num) |
10 | | -{ |
| 6 | +int buscaBinaria(int *vetor, int n, int num) { |
11 | 7 | int ini = 0, fim = n - 1, meio; |
12 | 8 | while (ini <= fim) { |
13 | | - meio = (ini + fim) / 2; |
14 | | - if (vetor[meio] == num) |
15 | | - { |
| 9 | + meio = ini + ((fim - ini) >> 1); |
| 10 | + if (vetor[meio] == num) { |
16 | 11 | return meio; |
17 | | - } else if (vetor[meio] < num) |
18 | | - { |
| 12 | + } else if (vetor[meio] < num) { |
19 | 13 | ini = meio + 1; |
20 | 14 | } else |
21 | | - { |
22 | 15 | fim = meio - 1; |
23 | | - } |
24 | 16 | } |
25 | 17 | return -1; |
26 | 18 | } |
27 | 19 | ////////////////////////////////////////////////////// |
28 | | -int main(void) |
29 | | -{ |
| 20 | +int main(void) { |
30 | 21 | int caixas, x, y, i, j, num, k, lotes[10001], posicao_inicial, posicao_final, n; |
31 | 22 |
|
32 | | - while (scanf("%d", &caixas) != EOF) |
33 | | - { |
| 23 | + while (scanf("%d", &caixas) != EOF) { |
34 | 24 | n = 0; |
35 | | - for (i = 0; i < 10001; i++) |
36 | | - { |
37 | | - lotes[i] = 0; |
38 | | - } |
39 | 25 |
|
40 | | - for (i = 0; i < caixas; i++) |
41 | | - { |
| 26 | + memset(lotes, 0, sizeof(lotes)); |
| 27 | + |
| 28 | + for (i = 0; i < caixas; i++) { |
42 | 29 | scanf("%d %d", &x, &y); |
43 | | - for (j = x; j <= y; j++) |
44 | | - { |
45 | | - lotes[n++] = j; |
46 | | - } |
| 30 | + for (j = x; j <= y; j++) lotes[n++] = j; |
47 | 31 | } |
48 | 32 |
|
49 | 33 | qsort(lotes, n, sizeof(int), cmp); |
50 | 34 |
|
51 | 35 | scanf("%d", &num); |
52 | 36 |
|
53 | | - if (buscaBinaria(lotes, n, num) == -1) |
54 | | - { |
| 37 | + if (buscaBinaria(lotes, n, num) == -1) { |
55 | 38 | printf("%d not found\n", num); |
56 | 39 | } else { |
57 | 40 | posicao_inicial = 0; |
58 | | - for (k = 0; k < n && lotes[k] <= num; k++) |
59 | | - { |
60 | | - if (lotes[k] == num) |
61 | | - { |
| 41 | + for (k = 0; k < n && lotes[k] <= num; k++) { |
| 42 | + if (lotes[k] == num) { |
62 | 43 | posicao_inicial = k; |
63 | 44 | break; |
64 | | - } else { |
| 45 | + } else |
65 | 46 | posicao_inicial += lotes[k] != lotes[k+1]; |
66 | | - } |
67 | 47 | } |
68 | 48 | posicao_final = posicao_inicial; |
69 | | - while (posicao_final < n - 1 && lotes[posicao_final + 1] == num) |
70 | | - { |
71 | | - posicao_final++; |
72 | | - } |
| 49 | + while (posicao_final < n - 1 && lotes[posicao_final + 1] == num) posicao_final++; |
73 | 50 | printf("%d found from %d to %d\n", num, posicao_inicial, posicao_final); |
74 | 51 | } |
75 | 52 | } |
76 | | - |
77 | 53 | return 0; |
78 | 54 | } |
0 commit comments