#include <bits/stdc++.h> using namespace std; struct Mine { int position; int range; }; using Mines = vector<Mine>; struct TestCase { int mine_count; Mines mines; }; TestCase read_test_case(); void solve_test_case(const TestCase&); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(read_test_case()); } TestCase read_test_case() { TestCase test_case; cin >> test_case.mine_count; test_case.mines.resize(test_case.mine_count); for (auto& mine : test_case.mines) cin >> mine.position >> mine.range; return test_case; } int explosion_result(const Mines&, int detonation); void solve_test_case(const TestCase& test_case) { vector<int> explosion_masks(test_case.mine_count, 0); for (int i = 0; i < test_case.mine_count; i++) { int max_left, max_right; int mask = 0; max_left = max_right = test_case.mines[i].position; for (int j = 0; j < test_case.mine_count; j++) { if (i - j > 0 && test_case.mines[i - j].position >= max_left) { mask |= (1 << (i - j)); max_left = min(max_left, test_case.mines[i - j].position - test_case.mines[i - j].range); max_right = max(max_right, test_case.mines[i - j].position + test_case.mines[i - j].range); } if (i + j < test_case.mine_count && test_case.mines[i + j].position <= max_right) { mask |= (1 << (i + j)); max_left = min(max_left, test_case.mines[i + j].position - test_case.mines[i + j].range); max_right = max(max_right, test_case.mines[i + j].position + test_case.mines[i + j].range); } } explosion_masks[i] = mask; } set<int> explosions; for (int m = 0; m < (1 << test_case.mine_count); m++) { int e = 0; for (int i = 0; i < test_case.mine_count; i++) if (m & (1 << i)) e |= explosion_masks[i]; explosions.insert(e); } cout << explosions.size() << "\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include <bits/stdc++.h> using namespace std; struct Mine { int position; int range; }; using Mines = vector<Mine>; struct TestCase { int mine_count; Mines mines; }; TestCase read_test_case(); void solve_test_case(const TestCase&); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(read_test_case()); } TestCase read_test_case() { TestCase test_case; cin >> test_case.mine_count; test_case.mines.resize(test_case.mine_count); for (auto& mine : test_case.mines) cin >> mine.position >> mine.range; return test_case; } int explosion_result(const Mines&, int detonation); void solve_test_case(const TestCase& test_case) { vector<int> explosion_masks(test_case.mine_count, 0); for (int i = 0; i < test_case.mine_count; i++) { int max_left, max_right; int mask = 0; max_left = max_right = test_case.mines[i].position; for (int j = 0; j < test_case.mine_count; j++) { if (i - j > 0 && test_case.mines[i - j].position >= max_left) { mask |= (1 << (i - j)); max_left = min(max_left, test_case.mines[i - j].position - test_case.mines[i - j].range); max_right = max(max_right, test_case.mines[i - j].position + test_case.mines[i - j].range); } if (i + j < test_case.mine_count && test_case.mines[i + j].position <= max_right) { mask |= (1 << (i + j)); max_left = min(max_left, test_case.mines[i + j].position - test_case.mines[i + j].range); max_right = max(max_right, test_case.mines[i + j].position + test_case.mines[i + j].range); } } explosion_masks[i] = mask; } set<int> explosions; for (int m = 0; m < (1 << test_case.mine_count); m++) { int e = 0; for (int i = 0; i < test_case.mine_count; i++) if (m & (1 << i)) e |= explosion_masks[i]; explosions.insert(e); } cout << explosions.size() << "\n"; } |