GokulDeep

Hrad

Oct 9th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. package com.gokul.demo.Success;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Hard {
  8.  
  9. public static void main(String[] args) throws NumberFormatException, IOException {
  10. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11. int t = Integer.parseInt(br.readLine().trim());
  12. for (long o = 0; o < t; o++) {
  13. long n = Integer.parseInt(br.readLine().trim());
  14. String[] numberStrings = br.readLine().split(" ");
  15.  
  16. long[] num = new long[(int) n];
  17. long[] bi = new long[(int) n];
  18. for (int i = 0; i < n; i++) {
  19. num[i] = Integer.parseInt(numberStrings[i]);
  20. }
  21. String[] biStrings = br.readLine().split(" ");
  22. for (int i = 0; i < n; i++) {
  23. bi[i] = Integer.parseInt(biStrings[i]);
  24. }
  25. if (isSorted(num)) {
  26. System.out.println("yes");
  27. } else {
  28. int i = 0, j = 1;
  29. boolean f = findSort(num, bi, n, i, j);
  30. if (!f) {
  31. System.out.println("no");
  32. }
  33. }
  34. }
  35. }
  36.  
  37. private static boolean findSort(long[] num, long[] bi, long n, int i, int j) {
  38.  
  39. for (; i < n; i++) {
  40. for (; j < n; j++) {
  41. if (num[i] > num[j] && bi[i] != bi[j]) {
  42. swap(i, j, num);
  43. swap(i, j, bi);
  44. if (isSorted(num)) {
  45. System.out.println("yes");
  46. return true;
  47. }
  48. if (findSort(num, bi, n, i + 1, j + 1)) {
  49. return true;
  50. }
  51.  
  52. swap(i, j, num);
  53. swap(i, j, bi);
  54. } else {
  55. if (findSort(num, bi, n, i + 1, j + 1)) {
  56. return true;
  57. }
  58. }
  59. }
  60. }
  61. return false;
  62. }
  63.  
  64. private static void swap(int i, int j, long[] num) {
  65.  
  66. long t = num[i];
  67. num[i] = num[j];
  68. num[j] = t;
  69.  
  70. }
  71.  
  72. private static boolean isSorted(long[] num) {
  73. // TODO Auto-generated method stub
  74. boolean f = true;
  75. for (int i = 0; i < num.length - 1; i++) {
  76. if (num[i] > num[i + 1]) {
  77. f = false;
  78. }
  79. }
  80. return f;
  81. }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment