結果
| 問題 |
No.1209 XOR Into You
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-09-06 18:36:05 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 533 ms / 2,000 ms |
| コード長 | 1,425 bytes |
| コンパイル時間 | 1,936 ms |
| コンパイル使用メモリ | 119,676 KB |
| 最終ジャッジ日時 | 2025-01-14 07:57:38 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 37 |
ソースコード
#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';
}