結果
| 問題 |
No.2165 Let's Play Nim!
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-12-24 04:32:15 |
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 187 ms / 2,000 ms |
| コード長 | 1,919 bytes |
| コンパイル時間 | 2,288 ms |
| コンパイル使用メモリ | 104,800 KB |
| 最終ジャッジ日時 | 2025-02-09 19:55:56 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 38 |
ソースコード
#include <array>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>
constexpr uint32_t L = 17;
struct PQ {
std::priority_queue<size_t> s, t;
void insert(size_t x) { s.push(x); }
void erase(size_t x) { t.push(x); }
size_t top() {
while (t.size() and s.top() == t.top()) s.pop(), t.pop();
return s.top();
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
size_t n;
std::cin >> n;
uint32_t x = 0;
std::vector<uint32_t> a(n);
std::array<PQ, L> pqs{};
for (size_t i = 0; i < n; ++i) {
std::cin >> a[i], x ^= a[i];
for (uint32_t j = 0; j < L; ++j) {
if ((a[i] >> j) & 1) pqs[j].insert(i);
}
}
bool t = x;
std::cout << t << '\n', std::cout.flush();
auto update = [&](size_t i, uint32_t k) {
for (uint32_t j = 0; j < L; ++j) {
if ((a[i] >> j) & 1) {
pqs[j].erase(i);
}
}
x ^= a[i];
a[i] -= k;
x ^= a[i];
for (uint32_t j = 0; j < L; ++j) {
if ((a[i] >> j) & 1) {
pqs[j].insert(i);
}
}
};
for (;; t = not t) {
if (t) {
assert(x);
for (uint32_t bit = L; bit-- > 0;) {
if ((x >> bit) & 1) {
size_t i = pqs[bit].top();
uint32_t k = a[i] - (x ^ a[i]);
assert(k and k <= a[i]);
update(i, k);
std::cout << i + 1 << ' ' << k << '\n', std::cout.flush();
break;
}
}
} else {
assert(x == 0);
size_t i;
uint32_t k;
std::cin >> i >> k;
--i;
update(i, k);
}
int ret;
std::cin >> ret;
if (ret == -1) break;
}
}