fork download
  1. import java.util.*;
  2. public class Main
  3. {
  4. public static void main(String args[])
  5. {
  6. Scanner sc = new Scanner(System.in);
  7. int t = sc.nextInt();
  8. while(t-->0)
  9. {
  10. int n = sc.nextInt();
  11. int m = sc.nextInt();
  12. List<Integer> res = new ArrayList<>();
  13. int[] a = new int[n];
  14. int[] b = new int[m];
  15. for(int i=0;i<n;i++)
  16. {
  17. a[i]=sc.nextInt();
  18. }
  19. for(int j=0;j<m;j++)
  20. {
  21. b[j]=sc.nextInt();
  22. }
  23. Arrays.sort(b);
  24. int d[] = new int[m];
  25. for(int i=0;i<m;i++)
  26. {
  27. d[i]=b[m-1-i];
  28. }
  29. int i=0,j=0;
  30. while(i<n && j<m)
  31. {
  32. if(a[i]>d[j])
  33. {
  34. res.add(a[i]);
  35. i++;
  36. }
  37. else
  38. {
  39. res.add(d[j]);
  40. j++;
  41. }
  42. }
  43. while(i<n)
  44. res.add(a[i++]);
  45. while(j<m)
  46. res.add(d[j++]);
  47. for(int x : res)
  48. System.out.print(x+" ");
  49. System.out.println();
  50. }
  51. }
  52. }
Success #stdin #stdout 0.18s 58996KB
stdin
7
2 1
6 4
5
5 5
1 7 2 4 5
5 4 1 2 7
1 9
7
1 2 3 4 5 6 7 8 9
3 2
1 3 5
2 4
10 5
1 9 2 3 8 1 4 7 2 9
7 8 5 4 6
2 1
2 2
1
6 1
1 1 1 1 1 1
777
stdout
6 5 4 
7 5 4 2 1 1 7 2 4 5 
9 8 7 7 6 5 4 3 2 1 
4 2 1 3 5 
8 7 6 5 4 1 9 2 3 8 1 4 7 2 9 
2 2 1 
777 1 1 1 1 1 1