結果

問題 No.1209 XOR Into You
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-08-30 14:04:51
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 2,846 bytes
コンパイル時間 1,105 ms
コンパイル使用メモリ 117,520 KB
実行使用メモリ 14,624 KB
最終ジャッジ日時 2023-09-04 09:39:15
合計ジャッジ時間 9,185 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 87 ms
14,624 KB
testcase_05 AC 87 ms
13,372 KB
testcase_06 AC 86 ms
13,392 KB
testcase_07 AC 78 ms
13,568 KB
testcase_08 AC 70 ms
11,172 KB
testcase_09 AC 69 ms
11,028 KB
testcase_10 AC 57 ms
12,452 KB
testcase_11 AC 69 ms
10,896 KB
testcase_12 AC 64 ms
12,564 KB
testcase_13 AC 73 ms
11,200 KB
testcase_14 AC 62 ms
12,064 KB
testcase_15 AC 65 ms
12,336 KB
testcase_16 AC 64 ms
13,136 KB
testcase_17 AC 62 ms
13,732 KB
testcase_18 AC 89 ms
14,460 KB
testcase_19 AC 64 ms
13,024 KB
testcase_20 AC 73 ms
12,540 KB
testcase_21 AC 56 ms
11,712 KB
testcase_22 AC 55 ms
13,836 KB
testcase_23 AC 55 ms
14,176 KB
testcase_24 AC 55 ms
14,208 KB
testcase_25 AC 91 ms
14,108 KB
testcase_26 AC 91 ms
14,188 KB
testcase_27 AC 91 ms
14,300 KB
testcase_28 AC 91 ms
14,132 KB
testcase_29 AC 92 ms
14,080 KB
testcase_30 AC 91 ms
14,040 KB
testcase_31 AC 96 ms
14,172 KB
testcase_32 AC 97 ms
14,152 KB
testcase_33 AC 96 ms
14,216 KB
testcase_34 AC 104 ms
14,512 KB
testcase_35 AC 97 ms
14,212 KB
testcase_36 AC 96 ms
14,196 KB
testcase_37 AC 58 ms
14,280 KB
testcase_38 AC 76 ms
11,452 KB
testcase_39 AC 68 ms
10,324 KB
testcase_40 AC 54 ms
14,220 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; }
real readReal() { return readToken.to!real; }

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)); }


void bAdd(T)(T[] bit, int pos, T val)
in {
  assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold");
}
do {
  for (int x = pos; x < bit.length; x |= x + 1) {
    bit[x] += val;
  }
}

// sum of [0, pos)
T bSum(T)(T[] bit, int pos)
in {
  assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold");
}
do {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {
    ret += bit[x];
  }
  return ret;
}


void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto A = new long[N];
      foreach (i; 0 .. N) {
        A[i] = readLong();
      }
      auto B = new long[N];
      foreach (i; 0 .. N) {
        B[i] = readLong();
      }
      
      bool ok = true;
      ok = ok && (A[0] == B[0]);
      ok = ok && (A[N - 1] == B[N - 1]);
      
      alias Entry = Tuple!(long, "d", int, "i");
      auto esA = new Entry[N - 1];
      auto esB = new Entry[N - 1];
      foreach (i; 0 .. N - 1) {
        esA[i] = Entry(A[i] ^ A[i + 1], i);
        esB[i] = Entry(B[i] ^ B[i + 1], i);
      }
      esA.sort;
      esB.sort;
      debug {
        writeln("esA = ", esA);
        writeln("esB = ", esB);
      }
      foreach (j; 0 .. N - 1) {
        ok = ok && (esA[j].d == esB[j].d);
      }
      
      if (ok) {
        auto vals = new int[N - 1];
        foreach (j; 0 .. N - 1) {
          vals[esA[j].i] = esB[j].i;
        }
        long ans;
        auto bit = new long[N - 1];
        foreach (i; 0 .. N - 1) {
          ans += i - bit.bSum(vals[i]);
          bit.bAdd(vals[i], +1);
        }
        writeln(ans);
      } else {
        writeln(-1);
      }
    }
  } catch (EOFException e) {
  }
}
0