CF981B Businessmen Problems map 模擬 二十二
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.
In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.
All elements are enumerated with integers. The ChemForces company has discovered nn distinct chemical elements with indices
The TopChemist company discovered mm distinct chemical elements with indices b1,b2,…,bmb1,b2,…,bm, and it will get an income of yjyj Berland rubles for including the
In other words, the first company can present any subset of elements from {a1,a2,…,an}{a1,a2,…,an} (possibly empty subset), the second company can present any subset of elements from {b1,b2,…,bm}{b1,b2,…,bm} (possibly empty subset). There shouldn‘t be equal elements in the subsets.
Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
InputThe first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of elements discovered by ChemForces.
The ii-th of the next nn lines contains two integers aiai and xixi (1≤ai≤1091≤ai≤109, 1≤xi≤1091≤xi≤109) — the index of the ii-th element and the income of its usage on the exhibition. It is guaranteed that all aiai are distinct.
The next line contains a single integer mm (1≤m≤1051≤m≤105) — the number of chemicals invented by TopChemist.
The jj-th of the next mm lines contains two integers bjbj and yjyj, (1≤bj≤1091≤bj≤109, 1≤yj≤1091≤yj≤109) — the index of the jj-th element and the income of its usage on the exhibition. It is guaranteed that all bjbj are distinct.
OutputPrint the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
Examples input Copy3output Copy
1 2
7 2
3 10
4
1 4
2 4
3 4
4 4
24input Copy
1output Copy
1000000000 239
3
14 15
92 65
35 89
408Note
In the first example ChemForces can choose the set (3,73,7), while TopChemist can choose (1,2,41,2,4). This way the total income is (10+2)+(4+4+4)=24(10+2)+(4+4+4)=24.
In the second example ChemForces can choose the only element 109109, while TopChemist can choose (14,92,3514,92,35). This way the total income is (239)+(15+65+89)=408(239)+(15+65+89)=408.
題意: 給你n個A公司的新元素及價值,再給你m個B公司的新元素及價值,兩個公司的元素相同的取較大值,問兩家公司元素價值總和
直接用一個map保存下相同元素的較大值,然後遍歷相加就可以了
#include <map> #include <set> #include <stack> #include <cmath> #include <queue> #include <cstdio> #include <vector> #include <string> #include <cstring> #include <iostream> #include <algorithm> #define debug(a) cout << #a << " " << a << endl using namespace std; const int maxn = 1e5 + 10; const int mod = 1e9 + 7; typedef long long ll; int main(){ std::ios::sync_with_stdio(false); ll n, m; while( cin >> n ) { map<ll,ll> mm; ll x, y; for( ll i = 0; i < n; i ++ ) { cin >> x >> y; if( mm[x] < y ) { mm[x] = y; } } cin >> m; for( ll i = 0; i < m; i ++ ) { cin >> x >> y; if( mm[x] < y ) { mm[x] = y; } } ll sum = 0; for( map<ll,ll>:: iterator it = mm.begin(); it != mm.end(); it ++ ) { sum += (*it).second; } cout << sum << endl; } return 0; }
CF981B Businessmen Problems map 模擬 二十二