-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrescate_aereo.cpp
153 lines (128 loc) · 3.58 KB
/
rescate_aereo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* @author PinkOBoy
*/
#include <iostream>
#include <vector>
using namespace std;
// - TIPOS PERSONALIZADOS -----------------------------------------------
// - DEFINED TYPES
struct tIntervalo {
int inicio; // Posicion de inicio del intervalo // Starting position of the segment
int fin; // Posicion de fin del intervalo // Ending position of the segment
};
// - PROTOTIPOS ---------------------------------------------------------
// - PROTOTYPES
/* PRECONDITION:
*
* 0 < v.size()
* ^
* EXISTE e : 0 <= e < v.size() : v[e] > altura
*/
tIntervalo resolver(
const vector<int>& edificios, // Alturas de los edificios // Buildings' height
int altura // Altura de vuelo del transbordador // Aircraft's flight height
); // return {a, b};
/* POSTCONDITION:
*
* MAX b - a : 0 <= a <= b < v.size() :
* (PARA_TODO w : a <= w <= b : v[w] > altura)
*/
// - MAIN ----------------------------------------------------------------
void solve() {
// read solution
int n, h;
cin >> n >> h;
vector<int> v(n);
for (int i = 0; i < n; ++i)
cin >> v[i];
// compute solution
tIntervalo sol = resolver(v, h);
// write solution
cout << sol.inicio << ' ' << sol.fin << endl;
}
int main() {
int c;
cin >> c;
for (int i = 0; i < c; ++i)
solve();
return 0;
}
// - IMPLEMENTACION -----------------------------------------------------
// - IMPLEMENTATION
/* COMPLEJIDAD DEL ALGORITMO // COMPLEXITY OF THE ALGORITHM
*
* - SPANISH -------------------------------------------------------------
* Todas las instrucciones ejecutadas tienen un coste de eficiencia constante.
* Existe un bucle en el algoritmo que ejecuta cierta cantidadd de instrucciones
* sobre todos los elementos del vector.
*
* Definiendo:
*
* n ::= numero de elementos del vector = edificios.size()
*
* Podemos afirmar que la complejidad de nuestro algoritmo sera de:
*
* O(n)
*
* - ENGLISH -------------------------------------------------------------
* Every executed instruction has a costant efficiency cost. A loop exists in the
* algorithm which executes certain amount of instructions on every vector's
* element.
*
* By defining:
*
* n ::= amount of elements of the vector = edificios.size()
*
* We can affirm the complexity of our algorithm is:
*
* O(n)
*/
tIntervalo resolver(
const vector<int>& edificios,
int altura
) {
tIntervalo sol = { 1, -1 }; // Default distance = -2
int headCurrentSegment = 0;
/* FUNCION DE COTA
* fc(x) = edificios.size() - i
*/
for (int i = 0; i < edificios.size(); ++i) {
// Si el transporte se puede ocultar tras el edificio
// If the aircraft can hide behind the building
if (edificios[i] > altura) {
// Si es el segmento mas largo encontrado
// If it is the longest segment found
if (i - headCurrentSegment > sol.fin - sol.inicio) {
sol.inicio = headCurrentSegment;
sol.fin = i;
}
}
/*
* Si no se puede ocultar, el segmento anterior queda
* cerrado y ya marcado como el mas largo, en caso de
* serlo.
*
* Ponemos el valor de inicio del segmento a la siguiente
* posicion por analizar.
*/
/*
* If it cannot hide, the segment ends and gets marked as
* the longest, if it actually is.
*
* We assign the next position to analyze to the head of
* the current segment
*/
else
headCurrentSegment = i + 1;
}
/* INVARIANTE
* 0 <= i <= v.size()
* ^
* -1 <= sol.inicio <= sol.fin <= i
* ^
* 0 <= headCurrentSegment
* ^
* PARA_TODO e : headCurrentSegment <= e <= i : v[e] > altura
*/
return sol;
}