結果

問題 No.1209 XOR Into You
ユーザー yudedakoyudedako
提出日時 2020-09-06 18:36:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 353 ms / 2,000 ms
コード長 1,425 bytes
コンパイル時間 1,717 ms
コンパイル使用メモリ 122,492 KB
実行使用メモリ 76,564 KB
最終ジャッジ日時 2023-08-19 14:13:40
合計ジャッジ時間 13,081 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 74 ms
4,376 KB
testcase_05 AC 75 ms
4,380 KB
testcase_06 AC 247 ms
75,824 KB
testcase_07 AC 276 ms
64,336 KB
testcase_08 AC 247 ms
58,176 KB
testcase_09 AC 241 ms
58,200 KB
testcase_10 AC 199 ms
48,320 KB
testcase_11 AC 254 ms
57,556 KB
testcase_12 AC 237 ms
53,684 KB
testcase_13 AC 268 ms
60,976 KB
testcase_14 AC 216 ms
52,220 KB
testcase_15 AC 231 ms
55,164 KB
testcase_16 AC 226 ms
54,240 KB
testcase_17 AC 214 ms
51,856 KB
testcase_18 AC 327 ms
73,260 KB
testcase_19 AC 224 ms
54,108 KB
testcase_20 AC 262 ms
61,236 KB
testcase_21 AC 185 ms
47,120 KB
testcase_22 AC 214 ms
76,412 KB
testcase_23 AC 214 ms
76,564 KB
testcase_24 AC 215 ms
76,496 KB
testcase_25 AC 338 ms
76,388 KB
testcase_26 AC 351 ms
76,428 KB
testcase_27 AC 353 ms
76,392 KB
testcase_28 AC 346 ms
76,512 KB
testcase_29 AC 348 ms
76,444 KB
testcase_30 AC 341 ms
76,556 KB
testcase_31 AC 111 ms
5,392 KB
testcase_32 AC 112 ms
5,412 KB
testcase_33 AC 112 ms
5,372 KB
testcase_34 AC 116 ms
5,460 KB
testcase_35 AC 111 ms
5,392 KB
testcase_36 AC 112 ms
5,464 KB
testcase_37 AC 83 ms
5,008 KB
testcase_38 AC 271 ms
63,732 KB
testcase_39 AC 237 ms
57,208 KB
testcase_40 AC 212 ms
76,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <iomanip>
#include <numeric>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <cassert>
#include <stack>


class Bit {
	std::vector<int> vec;
public:
	Bit(const int size) :vec(size, 0) {};
	void add(int position, const int value) {
		while (position < vec.size()) {
			vec[position] += value;
			position += ~position & (position + 1);
		}
	}
	int get(int position) const {
		int result{ 0 };
		while (position >= 0) {
			result += vec[position];
			position -= ~position & (position + 1);
		}
		return result;
	}
};

int main() {
	int n; std::cin >> n;
	std::vector<int> from(n), to(n);
	for (auto& a : from) std::cin >> a;
	for (auto& b : to) std::cin >> b;
	if (from.front() != to.front() || from.back() != to.back()) {
		std::cout << "-1\n";
		return 0;
	}
	std::map<int, std::queue<int>> map;
	for (auto i = 1; i < n; ++i) {
		map[from[i] ^ from[i - 1]].push(i - 1);
	}
	std::vector<int> rank; rank.reserve(n - 1);
	for (auto i = 1; i < n; ++i) {
		if (map[to[i] ^ to[i - 1]].empty()) {
			std::cout << "-1\n";
			return 0;
		}
		rank.push_back(map[to[i] ^ to[i - 1]].front());
		map[to[i] ^ to[i - 1]].pop();
	}
	Bit bit(n - 1);
	long long int result{ 0 };
	for (auto i = 0; i < rank.size(); ++i) {
		result += i - bit.get(rank[i]);
		bit.add(rank[i], 1);
	}
	std::cout << result << '\n';
}
0