結果

問題 No.1209 XOR Into You
ユーザー yudedakoyudedako
提出日時 2020-09-06 18:36:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 1,425 bytes
コンパイル時間 1,256 ms
コンパイル使用メモリ 124,280 KB
実行使用メモリ 76,800 KB
最終ジャッジ日時 2024-05-06 21:21:03
合計ジャッジ時間 10,836 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 75 ms
5,376 KB
testcase_05 AC 77 ms
5,376 KB
testcase_06 AC 212 ms
75,904 KB
testcase_07 AC 231 ms
64,512 KB
testcase_08 AC 203 ms
58,368 KB
testcase_09 AC 199 ms
58,496 KB
testcase_10 AC 159 ms
48,512 KB
testcase_11 AC 193 ms
57,728 KB
testcase_12 AC 188 ms
54,016 KB
testcase_13 AC 226 ms
60,928 KB
testcase_14 AC 175 ms
52,480 KB
testcase_15 AC 189 ms
55,168 KB
testcase_16 AC 187 ms
54,272 KB
testcase_17 AC 180 ms
52,096 KB
testcase_18 AC 276 ms
73,344 KB
testcase_19 AC 181 ms
54,272 KB
testcase_20 AC 221 ms
61,184 KB
testcase_21 AC 155 ms
47,360 KB
testcase_22 AC 198 ms
76,672 KB
testcase_23 AC 200 ms
76,672 KB
testcase_24 AC 195 ms
76,672 KB
testcase_25 AC 276 ms
76,800 KB
testcase_26 AC 280 ms
76,672 KB
testcase_27 AC 279 ms
76,544 KB
testcase_28 AC 277 ms
76,672 KB
testcase_29 AC 285 ms
76,672 KB
testcase_30 AC 285 ms
76,672 KB
testcase_31 AC 106 ms
5,504 KB
testcase_32 AC 114 ms
5,504 KB
testcase_33 AC 108 ms
5,504 KB
testcase_34 AC 112 ms
5,504 KB
testcase_35 AC 107 ms
5,504 KB
testcase_36 AC 109 ms
5,504 KB
testcase_37 AC 81 ms
5,376 KB
testcase_38 AC 216 ms
64,000 KB
testcase_39 AC 192 ms
57,216 KB
testcase_40 AC 195 ms
76,672 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