fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void merge(vector<int> &A, vector<int> &B){
  4. int i = 0,j = 0;
  5. //vector<int> ans;
  6. while(i<A.size() && j<B.size()){
  7. if(A[i] == B[j]){
  8. //ans.push_back(A[i]);
  9. //ans.push_back(B[j]);
  10. cout<<A[i]<<" "<<A[i]<<" ";
  11. i++;j++;
  12. continue;
  13. }else if(A[i]>B[j]){
  14. //ans.push_back(B[j]);
  15. cout<<B[j]<<" ";
  16. j++;
  17. continue;
  18. }else{
  19. cout<<A[i]<<" ";
  20. //ans.push_back(A[i]);
  21. i++;
  22. continue;
  23. }
  24. }
  25. //cout<<i<<" "<<j<<endl;
  26. while(i<A.size()){
  27. cout<<A[i]<<" ";
  28. //ans.push_back(A[i]);
  29. i++;
  30. }
  31. while(j<B.size()){
  32. //ans.push_back(B[j]);
  33. cout<<B[j]<<" ";
  34. j++;
  35. }
  36. cout<<endl;
  37. }
  38. int main() {
  39. int n,m;
  40. cin>>n>>m;
  41. vector<int> a,b;
  42. for(int i = 0; i<n; i++){
  43. int x;
  44. cin>>x;
  45. a.push_back(x);
  46. }
  47. for(int i = 0; i<m; i++){
  48. int x;
  49. cin>>x;
  50. b.push_back(x);
  51. }
  52. merge(a,b);
  53. return 0;
  54. }
Success #stdin #stdout 0s 15240KB
stdin
3 2
1 5 8 
6 9
stdout
1 5 6 8 9