fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void swap(int* a,int* b)
  4. {
  5. int temp=*a;
  6. *a=*b;
  7. *b=temp;
  8. }
  9. int partition(int arr[],int l,int r)
  10. {
  11. int pivot=arr[l];
  12. int j=l;
  13.  
  14. for(int i=l+1;i<=r;i++)
  15. {
  16. if(arr[i]<pivot)
  17. {
  18. j++;
  19. swap(&arr[i],&arr[j]);
  20. }
  21. }
  22.  
  23. swap(&arr[l],&arr[j]);
  24. return j;
  25. }
  26. void quicksort(int arr[],int l,int r)
  27. {
  28. if(l>=r) return;
  29. int p=partition(arr,l,r);
  30. quicksort(arr,l,p-1);
  31. quicksort(arr,p+1,r);
  32. }
  33. int main() {
  34. int t;
  35. cin>>t;
  36. while(t--)
  37. {
  38. int n,m;
  39. cin>>n>>m;
  40. int arr1[n],arr2[m],freq[m],rest[n],in=0,flag=0;
  41. map<int,int> farr;
  42. for(int i=0;i<n;i++)
  43. {
  44. cin>>arr1[i];
  45. farr[arr1[i]]++;
  46. }
  47. for(int i=0;i<m;i++)
  48. {
  49. cin>>arr2[i];
  50. // freq[i]=0;
  51. while(farr[arr2[i]]--)
  52. {
  53. // cout<<farr[arr2[i]]<<",";
  54. cout<<arr2[i]<<" ";
  55. }
  56. }
  57.  
  58. for(map<int,int>::iterator it=farr.begin();it!=farr.end();it++)
  59. {
  60. if(it->second>=0)
  61. {
  62. // cout<<it->first<<" "<<it->second<<endl;
  63. while(it->second--)
  64. rest[in++]=it->first;
  65. }
  66. }
  67.  
  68.  
  69. quicksort(rest,0,in-1);
  70.  
  71. for(int i=0;i<in;i++)
  72. {
  73. cout<<rest[i]<<" ";
  74. }
  75. cout<<endl;
  76. }
  77.  
  78. return 0;
  79. }
Success #stdin #stdout 0s 15240KB
stdin
1
11 4
2 1 2 5 7 1 9 3 6 8 8
2 1 8 3
stdout
2 2 1 1 8 8 3 5 6 7 9