-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRelative Sorting.cpp
47 lines (47 loc) · 986 Bytes
/
Relative Sorting.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
#include<bits/stdc++.h>
using namespace std;
int main()
{
/* https://practice.geeksforgeeks.org/problems/relative-sorting/0 */
//code
int t;
cin>>t;
while(t--){
int m,n;
cin>>m;
cin>>n;
vector<int> v1;
vector<int> v2;
int temp;
for(int i=0;i<m;i++)
{
cin>>temp;
v1.push_back(temp);
}
for(int i=0;i<n;i++)
{
cin>>temp;
v2.push_back(temp);
}
map<int,int> m1;
map<int,int> m2;
vector<int> tp;
for(int i=0;i<n;i++)
m2[v2[i]]++;
for(int i=0;i<m;i++){
if(m2.find(v1[i])!=m2.end())
m1[v1[i]]++;
else
tp.push_back(v1[i]);
}
sort(tp.begin(),tp.end());
for(int i=0;i<n;i++){
for(int j=0;j<m1[v2[i]];j++)
cout<<v2[i]<<" ";
}
for(int i=0;i<tp.size();i++)
cout<<tp[i]<<" ";
cout<<endl;
}
return 0;
}