結果

問題 No.1209 XOR Into You
ユーザー lavoxlavox
提出日時 2021-10-02 10:38:20
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,874 bytes
コンパイル時間 2,758 ms
コンパイル使用メモリ 79,676 KB
実行使用メモリ 69,124 KB
最終ジャッジ日時 2024-07-20 04:20:18
合計ジャッジ時間 18,981 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
36,276 KB
testcase_01 AC 54 ms
36,152 KB
testcase_02 AC 53 ms
36,476 KB
testcase_03 AC 55 ms
36,612 KB
testcase_04 AC 126 ms
39,216 KB
testcase_05 AC 128 ms
39,280 KB
testcase_06 AC 338 ms
66,060 KB
testcase_07 AC 372 ms
61,936 KB
testcase_08 AC 355 ms
59,876 KB
testcase_09 AC 377 ms
59,824 KB
testcase_10 AC 303 ms
56,812 KB
testcase_11 AC 378 ms
57,680 KB
testcase_12 AC 310 ms
59,192 KB
testcase_13 AC 390 ms
60,544 KB
testcase_14 AC 351 ms
59,304 KB
testcase_15 AC 399 ms
61,280 KB
testcase_16 AC 373 ms
59,772 KB
testcase_17 AC 332 ms
61,156 KB
testcase_18 WA -
testcase_19 AC 357 ms
59,536 KB
testcase_20 AC 389 ms
58,608 KB
testcase_21 AC 290 ms
56,352 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 220 ms
48,944 KB
testcase_32 AC 255 ms
48,648 KB
testcase_33 AC 242 ms
48,820 KB
testcase_34 AC 253 ms
48,916 KB
testcase_35 AC 227 ms
46,864 KB
testcase_36 AC 224 ms
48,280 KB
testcase_37 AC 223 ms
48,572 KB
testcase_38 AC 394 ms
59,680 KB
testcase_39 AC 365 ms
57,564 KB
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

		int ans = 0;
		BinaryIndexedTree tree = new BinaryIndexedTree(N);
		for (int i = 0 ; i < N - 1; i++) {
			ans += 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()); }
	}
}
0