-
Notifications
You must be signed in to change notification settings - Fork 6
/
N max pair combinations.cpp
56 lines (28 loc) · 1 KB
/
N max pair combinations.cpp
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
vector<int> Solution::solve(vector<int> &A, vector<int> &B) {
priority_queue<pair<int,pair<int,int> >>p;
sort(A.begin(),A.end());
sort(B.begin(),B.end());
int end1 = A.size()-1,end2 = B.size()-1;
map<pair<int,int>,int> m;
p.push({A[end1]+B[end2],{end1,end2}});
m[{end1,end2}] = 1;
int st = 1;
vector<int> v;
while(st<=A.size()){
pair<int,pair<int,int>> tp = p.top();
v.push_back(tp.first);
p.pop();
pair<int,int> p1 = {tp.second.first-1,tp.second.second};
if(!m[p1]){
p.push({A[p1.first]+B[p1.second],p1});
m[p1] =1;
};
p1 = {tp.second.first,tp.second.second-1};
if(!m[p1]){
p.push({A[p1.first]+B[p1.second],p1});
m[p1] =1;
};
st++;
}
return v;
}