-
Notifications
You must be signed in to change notification settings - Fork 5
/
P16175_ca_F002B._Vectors_comprimits.cc
49 lines (43 loc) · 1.25 KB
/
P16175_ca_F002B._Vectors_comprimits.cc
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
#include <bits/stdc++.h>
using namespace std;
struct Parell {
int valor; // Diferent de zero
int pos; // Més gran o igual que zero
};
typedef vector<Parell> Vec_Com; // Ordenat per pos!
Vec_Com suma(const Vec_Com& v1, const Vec_Com& v2) {
size_t j = 0;
Vec_Com res;
for (size_t i = 0; i < v1.size(); ++i) {
while (j < v2.size() and v2[j].pos < v1[i].pos) res.push_back(v2[j++]);
if (j < v2.size() and v1[i].pos == v2[j].pos) {
Parell p;
p.pos = v1[i].pos;
p.valor = v1[i].valor + v2[j].valor;
if (p.valor) res.push_back(p);
j++;
} else res.push_back(v1[i]);
}
for (; j < v2.size(); ++j) res.push_back(v2[j]);
return res;
}
void llegeix(Vec_Com& v) {
int n;
cin >> n;
v = Vec_Com(n);
char shit;
for (int i = 0; i < n; ++i) cin >> v[i].valor >> shit >> v[i].pos;
}
int main () {
int n;
cin >> n;
while (n--) {
Vec_Com v1, v2;
llegeix(v1);
llegeix(v2);
Vec_Com res = suma(v1,v2);
cout << res.size();
for (size_t i = 0; i < res.size(); ++i) cout << ' ' << res[i].valor << ';' << res[i].pos;
cout << endl;
}
}