-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06.cpp
More file actions
95 lines (80 loc) · 2.1 KB
/
06.cpp
File metadata and controls
95 lines (80 loc) · 2.1 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
// 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 personas, PriorityQueue<datos, ComparaDatos> cola, unsigned long int envios) {
datos dato1;
for (unsigned long int i = 0; i < envios; i++) {
dato1 = cola.top();
cola.pop();
std::cout << dato1.id << "\n";
dato1.toca += dato1.periodo;
cola.push(dato1);
}
}
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 personas = 0;
std::cin >> personas;
if (personas == 0)
return false;
auto cola = leer(personas);
unsigned long int envios;
std::cin >> envios;
resolver(personas,cola,envios);
std::cout << "----\n";
return true;
}
int main() {
// ajustes para que cin extraiga directamente de un fichero
#ifndef DOMJUDGE
std::ifstream in("Casos06.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;
}