File size: 1,051 Bytes
f7ba5f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
const int LIM = 500000;
int N, K;
vector<int> A, B;
string solve() {
bool is_equal = A == B;
// Check special cases.
if (K == 0) {
return is_equal ? "YES" : "NO";
}
if (K == 1 && is_equal) {
return "NO";
}
if (N == 2) {
if (is_equal) {
return K % 2 == 0 ? "YES" : "NO";
}
return K % 2 == 0 ? "NO" : "YES";
}
// Check array is rotated.
int num_out_of_order = 0;
unordered_map<int, int> pos;
for (int i = 0; i < N; i++) {
pos[A[i]] = i;
}
for (int i = 0; i < N; i++) {
if (pos[B[i]] > pos[B[(i + 1) % N]]) {
num_out_of_order++;
}
}
return num_out_of_order == 1 ? "YES" : "NO";
}
int main() {
int T;
cin >> T;
for (int t = 1; t <= T; t++) {
cin >> N >> K;
A.resize(N);
B.resize(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
for (int i = 0; i < N; i++) {
cin >> B[i];
}
cout << "Case #" << t << ": " << solve() << endl;
}
return 0;
}
|