-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonestar_day5.cpp
More file actions
94 lines (77 loc) · 2.25 KB
/
onestar_day5.cpp
File metadata and controls
94 lines (77 loc) · 2.25 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
#include <bits/stdc++.h>
using namespace std;
bool valid(const vector<int>& actualizacion, const unordered_map<int, unordered_set<int>>& antes) {
unordered_map<int, int> coordenada;
for (const auto& pagActual : actualizacion) {
coordenada[pagActual] = 0;
}
for (const auto& pagActual : actualizacion) {
if (antes.find(pagActual) != antes.end()) {
for (const auto& pagSig : antes.at(pagActual)) {
if (coordenada.find(pagSig) != coordenada.end()) {
coordenada[pagSig]++;
}
}
}
}
queue<int> q;
for (const auto& entry : coordenada) {
if (entry.second == 0) {
q.push(entry.first);
}
}
vector<int> ordenados;
while (!q.empty()) {
int pagActual = q.front();
q.pop();
ordenados.push_back(pagActual);
for (const auto& pagSig : antes.at(pagActual)) {
coordenada[pagSig]--;
if (coordenada[pagSig] == 0) {
q.push(pagSig);
}
}
}
return ordenados == actualizacion;
}
int fnMediaPag(const vector<int>& actualizacion) {
int n = actualizacion.size();
return actualizacion[n / 2];
}
int main() {
vector<pair<int, int>> reglas1 = {};
vector<vector<int>> reglas2 = {};
string renglon = "";
while (getline(cin, renglon)) {
istringstream iss(renglon);
vector<int> par;
int num;
while (iss >> num) {
par.push_back(num);
}
reglas1.push_back({par[0], par[1]});
}
string renglonDiff = "";
while (getline(cin, renglonDiff)) {
istringstream iss(renglonDiff);
vector<int> par;
int num;
while (iss >> num) {
par.push_back(num);
}
reglas2.push_back(par);
}
unordered_map<int, unordered_set<int>> antes;
for (const auto& rule : reglas1) {
antes[rule.first].insert(rule.second);
}
int sumaMediaPag = 0;
for (const auto& actualizacion : reglas2) {
if (valid(actualizacion, antes)) {
int middlepagActual = fnMediaPag(actualizacion);
sumaMediaPag += middlepagActual;
}
}
cout << sumaMediaPag << endl;
return 0;
}