結果
問題 | No.1209 XOR Into You |
ユーザー | lavox |
提出日時 | 2021-10-02 10:40:28 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 403 ms / 2,000 ms |
コード長 | 3,881 bytes |
コンパイル時間 | 3,339 ms |
コンパイル使用メモリ | 79,236 KB |
実行使用メモリ | 68,136 KB |
最終ジャッジ日時 | 2024-07-20 04:22:46 |
合計ジャッジ時間 | 16,419 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
36,496 KB |
testcase_01 | AC | 49 ms
36,220 KB |
testcase_02 | AC | 50 ms
36,452 KB |
testcase_03 | AC | 48 ms
36,220 KB |
testcase_04 | AC | 118 ms
39,028 KB |
testcase_05 | AC | 120 ms
39,536 KB |
testcase_06 | AC | 341 ms
66,532 KB |
testcase_07 | AC | 341 ms
59,852 KB |
testcase_08 | AC | 378 ms
59,464 KB |
testcase_09 | AC | 346 ms
59,444 KB |
testcase_10 | AC | 291 ms
54,528 KB |
testcase_11 | AC | 373 ms
59,208 KB |
testcase_12 | AC | 340 ms
59,644 KB |
testcase_13 | AC | 356 ms
60,464 KB |
testcase_14 | AC | 307 ms
59,164 KB |
testcase_15 | AC | 329 ms
61,904 KB |
testcase_16 | AC | 300 ms
59,856 KB |
testcase_17 | AC | 282 ms
59,348 KB |
testcase_18 | AC | 367 ms
66,996 KB |
testcase_19 | AC | 319 ms
59,544 KB |
testcase_20 | AC | 349 ms
60,728 KB |
testcase_21 | AC | 269 ms
53,812 KB |
testcase_22 | AC | 281 ms
66,136 KB |
testcase_23 | AC | 304 ms
66,656 KB |
testcase_24 | AC | 295 ms
66,384 KB |
testcase_25 | AC | 362 ms
66,584 KB |
testcase_26 | AC | 373 ms
68,136 KB |
testcase_27 | AC | 375 ms
67,508 KB |
testcase_28 | AC | 403 ms
68,128 KB |
testcase_29 | AC | 386 ms
66,512 KB |
testcase_30 | AC | 355 ms
66,484 KB |
testcase_31 | AC | 204 ms
47,112 KB |
testcase_32 | AC | 226 ms
47,252 KB |
testcase_33 | AC | 226 ms
48,912 KB |
testcase_34 | AC | 219 ms
47,256 KB |
testcase_35 | AC | 206 ms
47,076 KB |
testcase_36 | AC | 203 ms
47,072 KB |
testcase_37 | AC | 216 ms
48,816 KB |
testcase_38 | AC | 339 ms
61,796 KB |
testcase_39 | AC | 304 ms
57,680 KB |
testcase_40 | AC | 277 ms
66,088 KB |
ソースコード
import java.io.IOException; import java.io.InputStream; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.HashMap; import java.util.NoSuchElementException; public class Main { public static void main(String[] args) { Main o = new Main(); o.solve(); } public void solve() { FastScanner sc = new FastScanner(System.in); int N = sc.nextInt(); int[] A = new int[N]; int[] B = new int[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } for (int i = 0; i < N; i++) { B[i] = sc.nextInt(); } if ( A[0] != B[0] || A[N - 1] != B[N - 1] ) { System.out.println(-1); return; } int[] XA = new int[N - 1]; int[] XB = new int[N - 1]; for (int i = 0; i < N - 1; i++) { XA[i] = A[i] ^ A[i + 1]; XB[i] = B[i] ^ B[i + 1]; } HashMap<Integer, ArrayDeque<Integer>> map = new HashMap<>(); for (int i = 0; i < N - 1; i++) { if ( !map.containsKey(XA[i]) ) { map.put(XA[i], new ArrayDeque<>()); } map.get(XA[i]).addLast(i); } int[] YB = new int[N - 1]; boolean flg = true; for (int i = 0; i < N - 1; i++) { if ( map.containsKey(XB[i]) ) { ArrayDeque<Integer> que = map.get(XB[i]); if ( que.size() > 0 ) { YB[i] = que.pollFirst(); } else { flg = false; break; } } else { flg = false; break; } } if ( !flg ) { System.out.println(-1); return; } long ans = 0; BinaryIndexedTree tree = new BinaryIndexedTree(N); for (int i = 0 ; i < N - 1; i++) { ans += (long)i - tree.sum(YB[i] + 1); tree.add(YB[i] + 1, 1); } System.out.println(ans); } class BinaryIndexedTree { // 個数(max + 1) int N = 1; long[] tree = null; // 1-indexed BinaryIndexedTree(int max) { this.N = max + 1; tree = new long[N]; } // i番目(1-indexed)の要素にaを加える void add(int i, long a) { if ( i == 0 ) return; while ( i < N ) { tree[i] += a; i += i & -i; } } // 1〜i番目(含む)の和 long sum(int i) { long ret = 0; if ( i == 0 ) return 0; while ( i > 0 ) { ret += tree[i]; i -= (i & -i); } return ret; } } class FastScanner { private final InputStream in; private final byte[] buf = new byte[1024]; private int ptr = 0; private int buflen = 0; FastScanner( InputStream source ) { this.in = source; } private boolean hasNextByte() { if ( ptr < buflen ) return true; else { ptr = 0; try { buflen = in.read(buf); } catch (IOException e) { e.printStackTrace(); } if ( buflen <= 0 ) return false; } return true; } private int readByte() { if ( hasNextByte() ) return buf[ptr++]; else return -1; } private boolean isPrintableChar( int c ) { return 33 <= c && c <= 126; } private boolean isNumeric( int c ) { return '0' <= c && c <= '9'; } private void skipToNextPrintableChar() { while ( hasNextByte() && !isPrintableChar(buf[ptr]) ) ptr++; } public boolean hasNext() { skipToNextPrintableChar(); return hasNextByte(); } public String next() { if ( !hasNext() ) throw new NoSuchElementException(); StringBuilder ret = new StringBuilder(); int b = readByte(); while ( isPrintableChar(b) ) { ret.appendCodePoint(b); b = readByte(); } return ret.toString(); } public long nextLong() { if ( !hasNext() ) throw new NoSuchElementException(); long ret = 0; int b = readByte(); boolean negative = false; if ( b == '-' ) { negative = true; if ( hasNextByte() ) b = readByte(); } if ( !isNumeric(b) ) throw new NumberFormatException(); while ( true ) { if ( isNumeric(b) ) ret = ret * 10 + b - '0'; else if ( b == -1 || !isPrintableChar(b) ) return negative ? -ret : ret; else throw new NumberFormatException(); b = readByte(); } } public int nextInt() { return (int)nextLong(); } public double nextDouble() { return Double.parseDouble(next()); } } }