-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.cpp
More file actions
123 lines (103 loc) · 2.76 KB
/
07.cpp
File metadata and controls
123 lines (103 loc) · 2.76 KB
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
// Grupo 37, Víctor del Pino
// Comentario general sobre la solución,
// explicando cómo se resuelve el problema
#include <iostream>
#include <fstream>
#include <algorithm>
#include "PriorityQueue.h"
typedef struct {
unsigned long int id;
unsigned long int periodo;
unsigned long int toca;
}datos;
class ComparaDatos {
public:
bool operator()(datos a, datos b) {
if (a.toca != b.toca)
return a.toca < b.toca;
else
return a.id < b.id;
}
};
// función que resuelve el problema
// comentario sobre el coste, O(f(N)), donde N es el numero de nodos del arbol ya que los recorre todos para averiguarlo.
void resolver(unsigned long int primerPajaro, unsigned long int parejas) {
PriorityQueue<unsigned long int, std::greater<unsigned long int>> menores;
PriorityQueue<unsigned long int, std::less<unsigned long int>> mayores;
unsigned long int cabecera=primerPajaro,pajaroNuevo;
for (unsigned long int i = 0; i < parejas; i++) {
//primer pajaro
std::cin >> pajaroNuevo;
if (pajaroNuevo < cabecera)
menores.push(pajaroNuevo);
else
mayores.push(pajaroNuevo);
//segundo pajaro
std::cin >> pajaroNuevo;
if (pajaroNuevo < cabecera)
menores.push(pajaroNuevo);
else
mayores.push(pajaroNuevo);
if (menores.size() > mayores.size()) {
mayores.push(cabecera);
cabecera = menores.top();
menores.pop();
}
else if (mayores.size() > menores.size()) {
menores.push(cabecera);
cabecera = mayores.top();
mayores.pop();
}
std::cout << cabecera;
if (i!=parejas-1)
std::cout << " ";
}
}
//PriorityQueue<datos,ComparaDatos> leer(unsigned long int elementos) {
// PriorityQueue<datos,ComparaDatos> resultado = PriorityQueue<datos,ComparaDatos>();
// unsigned long int datoId=0;
// unsigned long int datoPeriodo=0;
// for (size_t i = 0; i < elementos; i++) {
// std::cin >> datoId;
// std::cin >> datoPeriodo;
//
// datos d;
// d.id = datoId;
// d.periodo = datoPeriodo;
// d.toca = datoPeriodo;
//
// resultado.push(d);
// }
// return resultado;
//}
// Resuelve un caso de prueba, leyendo de la entrada la
// configuración, y escribiendo la respuesta
bool resuelveCaso() {
unsigned long int primerPajaro = 0;
std::cin >> primerPajaro;
unsigned long int parejas= 0;
std::cin >> parejas;
if (primerPajaro == 0 && parejas ==0)
return false;
resolver(primerPajaro,parejas);
std::cout << "\n";
return true;
}
int main() {
// ajustes para que cin extraiga directamente de un fichero
#ifndef DOMJUDGE
std::ifstream in("Casos07.txt");
auto cinbuf = std::cin.rdbuf(in.rdbuf());
#endif
//int numCasos;
//std::cin >> numCasos;
//for (int i = 0; i < numCasos; ++i)
// resuelveCaso();
while (resuelveCaso()) {}
// para dejar todo como estaba al principio
#ifndef DOMJUDGE
std::cin.rdbuf(cinbuf);
system("PAUSE");
#endif
return 0;
}