Files
java-competitive/archive/2017.01/2017.01.07 - unsorted/I.java
2019-03-15 13:47:54 +04:00

100 lines
1.8 KiB
Java

package chelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.InputReader;
import io.OutputWriter;
public class I {
int n;
int[] a, b;
Map<Integer, Integer> l1, l2;
void swap(int i, int j) {
int p1 = l1.get(a[i]);
int p2 = l2.get(a[i]);
if (p1 != i) {
int c = p1;
p1 = p2;
p2 = c;
}
int p3 = l1.get(b[j]);
int p4 = l2.get(b[j]);
if (p3 != n + j) {
int c = p3;
p3 = p4;
p4 = c;
}
l1.put(a[i], n + j);
l2.put(a[i], p2);
l1.put(b[j], i);
l2.put(b[j], p4);
int c = a[i];
a[i] = b[j];
b[j] = c;
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
n = in.nextInt();
a = in.nextIntArray(n);
b = in.nextIntArray(n);
l1 = new HashMap<>();
l2 = new HashMap<>();
for (int i = 0; i < n; i++) {
if (!l1.containsKey(a[i])) {
l1.put(a[i], i);
} else {
l2.put(a[i], i);
}
}
for (int i = 0; i < n; i++) {
if (!l1.containsKey(b[i])) {
l1.put(b[i], i + n);
} else {
l2.put(b[i], i + n);
}
}
List<String> actions = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = a[i];
int p1 = l1.get(t);
int p2 = l2.get(t);
if (p2 < p1) {
int c = p1;
p1 = p2;
p2 = c;
}
if (p2 < n) {
swap(p2, i);
actions.add((p2 + 1) + " " + (i + 1));
} else {
p2 -= n;
swap(p2, p2);
actions.add((p2 + 1) + " " + (p2 + 1));
swap(p2, i);
actions.add((p2 + 1) + " " + (i + 1));
}
}
out.println(actions.size());
for (String action : actions) {
out.println(action);
}
}
}