Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gokul.demo.Success;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class Hard {
- public static void main(String[] args) throws NumberFormatException, IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int t = Integer.parseInt(br.readLine().trim());
- for (long o = 0; o < t; o++) {
- long n = Integer.parseInt(br.readLine().trim());
- String[] numberStrings = br.readLine().split(" ");
- long[] num = new long[(int) n];
- long[] bi = new long[(int) n];
- for (int i = 0; i < n; i++) {
- num[i] = Integer.parseInt(numberStrings[i]);
- }
- String[] biStrings = br.readLine().split(" ");
- for (int i = 0; i < n; i++) {
- bi[i] = Integer.parseInt(biStrings[i]);
- }
- if (isSorted(num)) {
- System.out.println("yes");
- } else {
- int i = 0, j = 1;
- boolean f = findSort(num, bi, n, i, j);
- if (!f) {
- System.out.println("no");
- }
- }
- }
- }
- private static boolean findSort(long[] num, long[] bi, long n, int i, int j) {
- for (; i < n; i++) {
- for (; j < n; j++) {
- if (num[i] > num[j] && bi[i] != bi[j]) {
- swap(i, j, num);
- swap(i, j, bi);
- if (isSorted(num)) {
- System.out.println("yes");
- return true;
- }
- if (findSort(num, bi, n, i + 1, j + 1)) {
- return true;
- }
- swap(i, j, num);
- swap(i, j, bi);
- } else {
- if (findSort(num, bi, n, i + 1, j + 1)) {
- return true;
- }
- }
- }
- }
- return false;
- }
- private static void swap(int i, int j, long[] num) {
- long t = num[i];
- num[i] = num[j];
- num[j] = t;
- }
- private static boolean isSorted(long[] num) {
- // TODO Auto-generated method stub
- boolean f = true;
- for (int i = 0; i < num.length - 1; i++) {
- if (num[i] > num[i + 1]) {
- f = false;
- }
- }
- return f;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment