git reimport
This commit is contained in:
97
archive/2017.01/2017.01.10 - unsorted/TaskK.java
Normal file
97
archive/2017.01/2017.01.10 - unsorted/TaskK.java
Normal file
@@ -0,0 +1,97 @@
|
||||
package chelper;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import io.InputReader;
|
||||
import io.OutputWriter;
|
||||
|
||||
public class TaskK {
|
||||
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
||||
int n = in.nextInt();
|
||||
int[] a = new int[n];
|
||||
int[] b = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
a[i] = in.nextInt() - 1;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
b[i] = in.nextInt() - 1;
|
||||
}
|
||||
int[][] position = new int[2][n];
|
||||
Arrays.fill(position[0], -1);
|
||||
for (int i = 0; i < n; i++) {
|
||||
int x = a[i];
|
||||
if (position[0][x] == -1) {
|
||||
position[0][x] = i;
|
||||
} else {
|
||||
position[1][x] = i;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int x = b[i];
|
||||
if (position[0][x] == -1) {
|
||||
position[0][x] = i + n;
|
||||
} else {
|
||||
position[1][x] = i + n;
|
||||
}
|
||||
}
|
||||
List<String> answer = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (a[i] == b[i]) {
|
||||
continue;
|
||||
}
|
||||
int x = a[i];
|
||||
int pos = position[0][x] == i ? position[1][x] : position[0][x];
|
||||
if (pos < n) {
|
||||
answer.add((pos + 1) + " " + (i + 1));
|
||||
int c = b[i];
|
||||
a[pos] = c;
|
||||
b[i] = x;
|
||||
if (position[0][x] == i) {
|
||||
position[1][x] = i + n;
|
||||
} else {
|
||||
position[0][x] = i + n;
|
||||
}
|
||||
if (position[0][c] == i + n) {
|
||||
position[0][c] = pos;
|
||||
} else {
|
||||
position[1][c] = pos;
|
||||
}
|
||||
} else {
|
||||
pos -= n;
|
||||
answer.add((pos + 1) + " " + (pos + 1));
|
||||
answer.add((pos + 1) + " " + (i + 1));
|
||||
int c = b[i];
|
||||
int d = a[pos];
|
||||
int posx1 = i;
|
||||
int posc = i + n;
|
||||
int posd = pos;
|
||||
int posx2 = pos + n;
|
||||
a[pos] = c;
|
||||
b[i] = x;
|
||||
b[pos] = d;
|
||||
if (position[0][x] == posx1) {
|
||||
position[1][x] = posc;
|
||||
} else {
|
||||
position[0][x] = posc;
|
||||
}
|
||||
if (position[0][c] == posc) {
|
||||
position[0][c] = posd;
|
||||
} else {
|
||||
position[1][c] = posd;
|
||||
}
|
||||
if (position[0][d] == posd) {
|
||||
position[0][d] = posx2;
|
||||
} else {
|
||||
position[1][d] = posx2;
|
||||
}
|
||||
}
|
||||
}
|
||||
out.println(answer.size());
|
||||
for (String s : answer) {
|
||||
out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user