結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 191 ms
24,428 KB
testcase_09 WA -
testcase_10 AC 57 ms
20,176 KB
testcase_11 WA -
testcase_12 AC 62 ms
18,728 KB
testcase_13 AC 62 ms
18,804 KB
testcase_14 AC 62 ms
18,896 KB
testcase_15 AC 63 ms
20,472 KB
testcase_16 AC 190 ms
24,368 KB
testcase_17 AC 189 ms
24,120 KB
testcase_18 AC 191 ms
25,140 KB
testcase_19 AC 195 ms
23,324 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 136 ms
24,168 KB
testcase_25 AC 142 ms
24,780 KB
testcase_26 AC 143 ms
24,732 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 39 ms
6,144 KB
testcase_29 AC 29 ms
8,764 KB
testcase_30 AC 76 ms
9,888 KB
testcase_31 AC 14 ms
5,348 KB
testcase_32 AC 17 ms
5,940 KB
testcase_33 AC 9 ms
5,844 KB
testcase_34 AC 36 ms
9,932 KB
testcase_35 AC 25 ms
10,096 KB
testcase_36 AC 160 ms
21,672 KB
testcase_37 AC 170 ms
23,056 KB
testcase_38 AC 11 ms
4,728 KB
testcase_39 AC 147 ms
14,452 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 21 ms
7,220 KB
testcase_42 AC 13 ms
5,584 KB
testcase_43 AC 38 ms
9,996 KB
testcase_44 AC 58 ms
18,544 KB
testcase_45 AC 109 ms
11,896 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 (N % 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