#include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; using std::int8_t; using std::int64_t; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Painting { int value[2] = {}; }; // read_input int num_paintings; std::vector<Painting> paintings; int target_count_0; // find_initial_solution std::vector<int8_t> initial_solution; int64_t initial_score; int initial_count_0; int update_to_whom; int num_required_updates; // solve std::vector<int8_t> solution; std::vector<int8_t> good_solution; int64_t good_solution_score; void read_input() { std::cin >> num_paintings >> target_count_0; paintings.resize(num_paintings); REP(i, 2) { for (Painting &p : paintings) { std::cin >> p.value[i]; } } } void find_initial_solution() { initial_solution.reserve(num_paintings); initial_score = 0; initial_count_0 = 0; int64_t sum = 0; for (const Painting &p : paintings) { const int who = p.value[0] <= p.value[1] ? 0 : 1; if (who == 0) ++initial_count_0; initial_solution.push_back(who); sum += p.value[who]; sum = std::max<int64_t>(sum, 0); initial_score = std::max(initial_score, sum); } } void find_updates() { if (initial_count_0 <= target_count_0) { update_to_whom = 0; num_required_updates = target_count_0 - initial_count_0; } else { update_to_whom = 1; num_required_updates = initial_count_0 - target_count_0; } } struct Increment { int index = 0; int value = 0; }; inline bool operator<(const Increment &a, const Increment &b) { return a.value < b.value; } bool try_solve(const int64_t score_limit) { solution = initial_solution; int num_updates_left = num_required_updates; if (num_updates_left == 0) return true; int64_t sum = 0; std::multiset<Increment> increments; int64_t increments_total = 0; REP(painting_idx, num_paintings) { const Painting &p = paintings[painting_idx]; const int current = initial_solution[painting_idx]; const int val = p.value[current]; sum += val; if (current != update_to_whom) { Increment increment; increment.index = painting_idx; increment.value = p.value[update_to_whom] - val; increments.insert(increment); increments_total += increment.value; } while (sum < 0) { auto it = increments.begin(); if (it == increments.end()) { sum = 0; } else { Increment increment = *it; increments.erase(it); sum += increment.value; increments_total -= increment.value; if (sum > 0) { increment.value = sum; sum = 0; increments.insert(increment); increments_total += increment.value; } else { solution[increment.index] = update_to_whom; --num_updates_left; if (num_updates_left == 0) return true; } } } while (sum + increments_total > score_limit) { auto it = increments.end(); assert (it != increments.begin()); --it; increments_total -= it->value; increments.erase(it); } } for (const auto &increment : increments) { solution[increment.index] = update_to_whom; --num_updates_left; if (num_updates_left == 0) return true; } return false; } void solve() { int64_t impossible_score = initial_score - 1; int64_t possible_score = 0; for (const Painting &p : paintings) { possible_score += std::max(std::max(p.value[0], p.value[1]), 0); } // Go 1 too far to make sure a solution actually gets generated. possible_score += 1; while (possible_score - impossible_score > 1) { const int64_t score = impossible_score + (possible_score - impossible_score) / 2; if (try_solve(score)) { good_solution = solution; good_solution_score = score; possible_score = score; } else { impossible_score = score; } } } void print_solution_score() { std::cout << good_solution_score << "\n"; } void print_solution() { for (const auto who : good_solution) { const char c = 'A' + who; std::cout << c; } std::cout << '\n'; } int main() { init_io(); read_input(); find_initial_solution(); find_updates(); solve(); print_solution_score(); print_solution(); }
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | #include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; using std::int8_t; using std::int64_t; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Painting { int value[2] = {}; }; // read_input int num_paintings; std::vector<Painting> paintings; int target_count_0; // find_initial_solution std::vector<int8_t> initial_solution; int64_t initial_score; int initial_count_0; int update_to_whom; int num_required_updates; // solve std::vector<int8_t> solution; std::vector<int8_t> good_solution; int64_t good_solution_score; void read_input() { std::cin >> num_paintings >> target_count_0; paintings.resize(num_paintings); REP(i, 2) { for (Painting &p : paintings) { std::cin >> p.value[i]; } } } void find_initial_solution() { initial_solution.reserve(num_paintings); initial_score = 0; initial_count_0 = 0; int64_t sum = 0; for (const Painting &p : paintings) { const int who = p.value[0] <= p.value[1] ? 0 : 1; if (who == 0) ++initial_count_0; initial_solution.push_back(who); sum += p.value[who]; sum = std::max<int64_t>(sum, 0); initial_score = std::max(initial_score, sum); } } void find_updates() { if (initial_count_0 <= target_count_0) { update_to_whom = 0; num_required_updates = target_count_0 - initial_count_0; } else { update_to_whom = 1; num_required_updates = initial_count_0 - target_count_0; } } struct Increment { int index = 0; int value = 0; }; inline bool operator<(const Increment &a, const Increment &b) { return a.value < b.value; } bool try_solve(const int64_t score_limit) { solution = initial_solution; int num_updates_left = num_required_updates; if (num_updates_left == 0) return true; int64_t sum = 0; std::multiset<Increment> increments; int64_t increments_total = 0; REP(painting_idx, num_paintings) { const Painting &p = paintings[painting_idx]; const int current = initial_solution[painting_idx]; const int val = p.value[current]; sum += val; if (current != update_to_whom) { Increment increment; increment.index = painting_idx; increment.value = p.value[update_to_whom] - val; increments.insert(increment); increments_total += increment.value; } while (sum < 0) { auto it = increments.begin(); if (it == increments.end()) { sum = 0; } else { Increment increment = *it; increments.erase(it); sum += increment.value; increments_total -= increment.value; if (sum > 0) { increment.value = sum; sum = 0; increments.insert(increment); increments_total += increment.value; } else { solution[increment.index] = update_to_whom; --num_updates_left; if (num_updates_left == 0) return true; } } } while (sum + increments_total > score_limit) { auto it = increments.end(); assert (it != increments.begin()); --it; increments_total -= it->value; increments.erase(it); } } for (const auto &increment : increments) { solution[increment.index] = update_to_whom; --num_updates_left; if (num_updates_left == 0) return true; } return false; } void solve() { int64_t impossible_score = initial_score - 1; int64_t possible_score = 0; for (const Painting &p : paintings) { possible_score += std::max(std::max(p.value[0], p.value[1]), 0); } // Go 1 too far to make sure a solution actually gets generated. possible_score += 1; while (possible_score - impossible_score > 1) { const int64_t score = impossible_score + (possible_score - impossible_score) / 2; if (try_solve(score)) { good_solution = solution; good_solution_score = score; possible_score = score; } else { impossible_score = score; } } } void print_solution_score() { std::cout << good_solution_score << "\n"; } void print_solution() { for (const auto who : good_solution) { const char c = 'A' + who; std::cout << c; } std::cout << '\n'; } int main() { init_io(); read_input(); find_initial_solution(); find_updates(); solve(); print_solution_score(); print_solution(); } |