#include <iostream> #include <vector> #include <utility> #include <cmath> #include <queue> const int INF = 1e9; void add(std::vector<long long>* points, int diff, int how_many) { points->push_back(-INF); int avg = diff / how_many; int overhead = diff - avg * how_many; int start = points->size(); for (int i = 0; i < how_many; i++) { points->push_back(avg); } std::priority_queue<std::pair<int, std::pair<int, int>>> segments; segments.push({how_many, {start, start + how_many - 1}}); for (int i = 0; i < abs(overhead); i++) { auto seg = segments.top(); segments.pop(); int mid = seg.second.first + seg.first / 2; points->operator[](mid) += std::copysign(1, overhead); segments.push({mid - seg.second.first, {seg.second.first, mid - 1}}); segments.push({seg.second.second - mid, {mid + 1, seg.second.second}}); } } bool check(const std::vector<long long>& points, const std::vector<int> diffs) { for (int i = 1; i <= diffs.size(); i++) { long long sum = 0; for (int j = 0; j < i; j++) { sum += points[j]; } if (sum > diffs[i - 1]) { return false; } for (int j = i; j < points.size(); j++) { sum -= points[j - i]; sum += points[j]; if (sum > diffs[i - 1]) { return false; } } } return true; } int main() { int n; std::cin >> n; std::vector<int> diffs; std::vector<long long> points; for (int i = 1; i <= n; i++) { int a; std::cin >> a; diffs.push_back(a); add(&points, a, i); } if (check(points, diffs)) { std::cout << "TAK\n"; std::cout << points.size() << '\n'; for (auto p: points) { std::cout << p << " "; } } else { std::cout << "NIE\n"; } return 0; };
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 | #include <iostream> #include <vector> #include <utility> #include <cmath> #include <queue> const int INF = 1e9; void add(std::vector<long long>* points, int diff, int how_many) { points->push_back(-INF); int avg = diff / how_many; int overhead = diff - avg * how_many; int start = points->size(); for (int i = 0; i < how_many; i++) { points->push_back(avg); } std::priority_queue<std::pair<int, std::pair<int, int>>> segments; segments.push({how_many, {start, start + how_many - 1}}); for (int i = 0; i < abs(overhead); i++) { auto seg = segments.top(); segments.pop(); int mid = seg.second.first + seg.first / 2; points->operator[](mid) += std::copysign(1, overhead); segments.push({mid - seg.second.first, {seg.second.first, mid - 1}}); segments.push({seg.second.second - mid, {mid + 1, seg.second.second}}); } } bool check(const std::vector<long long>& points, const std::vector<int> diffs) { for (int i = 1; i <= diffs.size(); i++) { long long sum = 0; for (int j = 0; j < i; j++) { sum += points[j]; } if (sum > diffs[i - 1]) { return false; } for (int j = i; j < points.size(); j++) { sum -= points[j - i]; sum += points[j]; if (sum > diffs[i - 1]) { return false; } } } return true; } int main() { int n; std::cin >> n; std::vector<int> diffs; std::vector<long long> points; for (int i = 1; i <= n; i++) { int a; std::cin >> a; diffs.push_back(a); add(&points, a, i); } if (check(points, diffs)) { std::cout << "TAK\n"; std::cout << points.size() << '\n'; for (auto p: points) { std::cout << p << " "; } } else { std::cout << "NIE\n"; } return 0; }; |