#include <bits/stdc++.h>
using namespace std;
optional<vector<int>> Solve(const vector<int>& a) {
const int n = static_cast<int>(a.size());
vector<int> b;
for (int i = 0; i < n; ++i) {
bool failed = false;
for (int j = 0; j < i; ++j) {
if (a[i] > a[j] + a[i - j - 1]) {
failed = true;
break;
}
}
if (failed) {
return nullopt;
}
int last = i == 0 ? 0 : a[i - 1];
b.push_back(a[i] - last);
}
return b;
}
int RandInt(int min_value, int max_value) {
return rand() % (max_value - min_value + 1) + min_value;
}
vector<int> GetValues(const vector<int>& a, int k) {
const int n = static_cast<int>(a.size());
vector<int> b;
for (int i = 1; i <= k; ++i) {
int max_value = -1e9;
for (int j = 0; j < n - i + 1; ++j) {
int sum = accumulate(a.begin() + j, a.begin() + j + i, 0);
max_value = max(max_value, sum);
}
b.push_back(max_value);
}
return b;
}
void Stress(int n, int k, int min_value, int max_value, int iters) {
for (int iter = 0; iter < iters; ++iter) {
vector<int> a(n);
for (int i = 0; i < n; ++i)
a[i] = RandInt(min_value, max_value);
auto correct_b = GetValues(a, k);
auto answer_a = Solve(correct_b);
if (!answer_a.has_value()) {
cout << "A: ";
for (int e: a) cout << e << ' '; cout << endl;
cout << "B: ";
for (int e: correct_b) cout << e << ' '; cout << endl;
assert(0);
}
auto answer_b = GetValues(answer_a.value(), k);
if (correct_b != answer_b) {
cout << "bad" << endl;
assert(0);
}
}
}
void RunStress() {
for (int n = 1; n <= 100; ++n) {
for (int k = 1; k <= n; ++k) {
int min_value = -1e6;
int max_value = +1e6;
int iters = 100;
Stress(n, k, min_value, max_value, iters);
cerr << n << ' ' << k << ' ' << min_value << ' ' << max_value << ' ' << iters << endl;
}
}
}
void SolveSio() {
int n;
cin >> n;
vector<int> b(n);
for (int& element: b)
cin >> element;
auto result = Solve(b);
if (!result.has_value()) {
cout << "NIE\n";
return;
}
cout << "TAK\n";
cout << result.value().size() << '\n';
for (int element: result.value()) {
cout << element << ' ';
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
SolveSio();
}
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 | #include <bits/stdc++.h> using namespace std; optional<vector<int>> Solve(const vector<int>& a) { const int n = static_cast<int>(a.size()); vector<int> b; for (int i = 0; i < n; ++i) { bool failed = false; for (int j = 0; j < i; ++j) { if (a[i] > a[j] + a[i - j - 1]) { failed = true; break; } } if (failed) { return nullopt; } int last = i == 0 ? 0 : a[i - 1]; b.push_back(a[i] - last); } return b; } int RandInt(int min_value, int max_value) { return rand() % (max_value - min_value + 1) + min_value; } vector<int> GetValues(const vector<int>& a, int k) { const int n = static_cast<int>(a.size()); vector<int> b; for (int i = 1; i <= k; ++i) { int max_value = -1e9; for (int j = 0; j < n - i + 1; ++j) { int sum = accumulate(a.begin() + j, a.begin() + j + i, 0); max_value = max(max_value, sum); } b.push_back(max_value); } return b; } void Stress(int n, int k, int min_value, int max_value, int iters) { for (int iter = 0; iter < iters; ++iter) { vector<int> a(n); for (int i = 0; i < n; ++i) a[i] = RandInt(min_value, max_value); auto correct_b = GetValues(a, k); auto answer_a = Solve(correct_b); if (!answer_a.has_value()) { cout << "A: "; for (int e: a) cout << e << ' '; cout << endl; cout << "B: "; for (int e: correct_b) cout << e << ' '; cout << endl; assert(0); } auto answer_b = GetValues(answer_a.value(), k); if (correct_b != answer_b) { cout << "bad" << endl; assert(0); } } } void RunStress() { for (int n = 1; n <= 100; ++n) { for (int k = 1; k <= n; ++k) { int min_value = -1e6; int max_value = +1e6; int iters = 100; Stress(n, k, min_value, max_value, iters); cerr << n << ' ' << k << ' ' << min_value << ' ' << max_value << ' ' << iters << endl; } } } void SolveSio() { int n; cin >> n; vector<int> b(n); for (int& element: b) cin >> element; auto result = Solve(b); if (!result.has_value()) { cout << "NIE\n"; return; } cout << "TAK\n"; cout << result.value().size() << '\n'; for (int element: result.value()) { cout << element << ' '; } cout << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); SolveSio(); } |
English