-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinMoves.cpp
50 lines (50 loc) · 1.31 KB
/
MinMoves.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
#include<bits/stdc++.h>
using namespace std;
int doInOrDe(vector<int>& arr_a, vector<int>& arr_b, int n, int m) {
int min=*min_element(arr_a.begin(),arr_a.end());
int max=*max_element(arr_b.begin(),arr_b.end());
int countmin=count(arr_a.begin(),arr_a.end(),min);
int countmax=count(arr_b.begin(),arr_b.end(),max);
int moves=0, i;
int diff=max-min;
if(countmin<=countmax) {
for(i=0;i<n&&countmin;i++) {
if(arr_a[i]==min) {
moves++;
countmin--;
arr_a[i]++;
}
}
}
else {
for(i=0;i<m&&countmax;i++) {
if(arr_b[i]==max) {
moves++;
countmax--;
arr_b[i]--;
}
}
}
return moves;
}
int getMinimumMoves(vector<int>& arr_a, vector<int>& arr_b) {
int moves=0, n=arr_a.size(), m=arr_b.size();
while(*min_element(arr_a.begin(),arr_a.end())<*max_element(arr_b.begin(),arr_b.end())) {
moves+=doInOrDe(arr_a,arr_b,n,m);
}
return moves;
}
using namespace std;
int main(void) {
int n, m, i;
cin>>n;
vector<int> ar_a(n);
for(i=0;i<n;i++)
cin>>ar_a[i];
cin>>m;
vector<int> ar_b(m);
for(i=0;i<m;i++)
cin>>ar_b[i];
cout<<endl<<getMinimumMoves(ar_a,ar_b)<<endl;
return 0;
}