結果

問題 No.2254 Reverse Only
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-03-24 23:56:59
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 2,766 bytes
コンパイル時間 6,075 ms
コンパイル使用メモリ 119,236 KB
実行使用メモリ 25,348 KB
最終ジャッジ日時 2023-09-04 20:05:09
合計ジャッジ時間 10,955 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 189 ms
23,532 KB
testcase_09 WA -
testcase_10 AC 58 ms
18,692 KB
testcase_11 WA -
testcase_12 AC 62 ms
18,828 KB
testcase_13 AC 62 ms
19,700 KB
testcase_14 AC 61 ms
18,564 KB
testcase_15 AC 62 ms
21,892 KB
testcase_16 AC 191 ms
24,208 KB
testcase_17 AC 191 ms
23,780 KB
testcase_18 AC 192 ms
24,856 KB
testcase_19 AC 194 ms
25,064 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 136 ms
23,368 KB
testcase_25 AC 142 ms
24,192 KB
testcase_26 AC 141 ms
23,728 KB
testcase_27 AC 3 ms
4,384 KB
testcase_28 AC 40 ms
6,160 KB
testcase_29 AC 29 ms
10,560 KB
testcase_30 AC 76 ms
10,376 KB
testcase_31 AC 15 ms
6,700 KB
testcase_32 AC 16 ms
6,028 KB
testcase_33 AC 8 ms
5,340 KB
testcase_34 AC 36 ms
10,856 KB
testcase_35 AC 25 ms
8,544 KB
testcase_36 AC 162 ms
22,508 KB
testcase_37 AC 168 ms
21,712 KB
testcase_38 AC 12 ms
4,756 KB
testcase_39 AC 147 ms
14,348 KB
testcase_40 AC 3 ms
4,384 KB
testcase_41 AC 21 ms
6,648 KB
testcase_42 AC 12 ms
5,948 KB
testcase_43 AC 38 ms
11,444 KB
testcase_44 AC 58 ms
18,380 KB
testcase_45 AC 109 ms
11,676 KB
testcase_46 AC 3 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


int N, K;
int[] A, B;

bool solve() {
  if (A == B) {
    return true;
  }
  if (K == 1) {
    return false;
  } else if (K == N) {
    auto revA = A.dup.reverse;
    if (revA == B) return true;
    return false;
  } else if (K > N) {
    return false;
  } else {
    alias Entry = Tuple!(int, "val", int, "pos");
    auto as = new Entry[N];
    auto bs = new Entry[N];
    foreach (i; 0 .. N) as[i] = Entry(A[i], i);
    foreach (i; 0 .. N) bs[i] = Entry(B[i], i);
    as.sort;
    bs.sort;
    auto ps = new int[N];
    foreach (i; 0 .. N) {
      if (as[i].val != bs[i].val) {
        return false;
      }
      ps[as[i].pos] = bs[i].pos;
    }
    if (K % 2 == 0) {
      return true;
    }
    foreach (i; 0 .. N - 1) {
      if (as[i].val == as[i + 1].val) {
        return true;
      }
    }
    auto uf = new int[N];
    uf[] = -1;
    foreach (i; 0 .. N) {
      uf.connect(i, ps[i]);
    }
    int numComps;
    foreach (i; 0 .. N) if (uf[i] < 0) {
      ++numComps;
    }
    if ((N - numComps) % 2 == 0) {
      return true;
    }
    return false;
  }
}

void main() {
  try {
    for (; ; ) {
      N = readInt;
      K = readInt;
      A = new int[N]; foreach (i; 0 .. N) A[i] = readInt;
      B = new int[N]; foreach (i; 0 .. N) B[i] = readInt;
      
      const ans = solve;
      writeln(ans ? "Yes" : "No");
    }
  } catch (EOFException e) {
  }
}
0