#include <bits/stdc++.h> #include <unistd.h> using namespace std; #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) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; #define BIGMOD 1000012177LL #define INF 10000000000000LL void print_vec(const vector<LL> &v) { for (LL x : v) { printf("%lld ", x); } } bool isValid(const vector<LL> &maxes, const vector<LL> &sol) { int n = maxes.size(); printf("maxes: "); print_vec(maxes); printf("\n"); printf("sol: "); print_vec(sol); printf("\n"); for (int substrLen=1; substrLen <= n; substrLen++) { LL mx = -INF; for (int i=0; i < sol.size()-substrLen+1; i++) { LL sum = 0; for (int j=0; j < substrLen; j++) { sum += sol[i+j]; } //printf("sum:%lld\n", sum); mx = max(mx, sum); } if (mx != maxes[substrLen-1]) { printf("solution differ at len %d, expected: %lld got: %lld\n\n", substrLen, maxes[substrLen-1], mx); return false; } } printf("ok\n\n"); return true; } vector<LL> solve(const vector<LL> &maxes) { vector<LL> answer; vector<LL> current; LL currentSum = 0; for (int L=1; L <= maxes.size(); L++) { // niezmiennik: (current.size() + 1 == L) and (current satisfies maxes[1..L-1]) LL m = maxes[L-1]; LL neededElem = m - currentSum; //printf("L=%d m=%lld, neededElem=%lld\n", L, m, neededElem); current.emplace_back(neededElem); currentSum += neededElem; // Now, current.size() == L // Now, verify that neededElem keeps invariant int currentLen = current.size(); LL sum = 0; for (int i=1; i <= L; i++) { sum += current[currentLen-i]; if (sum > maxes[i-1]) { //printf("sum %lld > maxes[%d]=%lld at length %d\n", sum, i, maxes[i-1], L); return vector<LL>(); } } answer.insert(answer.end(), current.begin(), current.end()); answer.emplace_back(-INF); } return answer; } bool testSolve(const vector<LL> &maxes) { return isValid(maxes, solve(maxes)); } void selftest() { //isValid({3,4,5,-1}, {2, 2, -7, 0, 3, -7, 3, -1, 3}); //isValid({3,4,5,-1}, {3,-100,3,1,-100,3,1,1,-100,3,1,1,-6}); testSolve({3,4,5,-1}); //isValid({3,1,4,1,5}, {3, -INF, 3, -2, -INF, 3, -2, 3, -INF, 3, -2, 3, -3, -INF, 3, -2, 3, -3, 3}); testSolve({3,1,4,1,5}); testSolve({1,2,3,4,5,6,7,8,9,-1,-2}); testSolve({9,2,3,4,5,6,7,8,9,-1,-2}); testSolve({9,2,3,4,5,6,7,8,9,-1,-2}); //FOR(a,-3,3) FOR(b,-3,3) FOR(c, -3,3) FOR(d, -3,3) // testSolve({a,b,c,d}); //isValid({3,2,-3,2}, {3,-INF,3,-1,-INF,3,-1,-5,-INF,0,0,0,2}); FOR(a,0,3) FOR(b,0,3) FOR(c, 0,3) FOR(d, 0,3) testSolve({a,b,c,d}); //isValid({2,0,2,1}, {2,-INF,0,0,2,-1}); } int main() { //selftest(); int n; scanf("%d", &n); vector<LL> maxes; REP(i, n) { LL x; scanf("%lld", &x); maxes.emplace_back(x); } auto answer = solve(maxes); if (answer.size() == 0) { printf("NIE\n"); } else { printf("TAK\n"); printf("%d\n", answer.size()); for (LL x : answer) { printf("%lld ", x); } printf("\n"); } return 0; } /* NIE: 1 2 1 3 2 0 3 3 2 0 2 2 2 0 0 1 1 3 0 0 1 0 0 1 1 0 0 2 */
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 | #include <bits/stdc++.h> #include <unistd.h> using namespace std; #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) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; typedef unsigned long long ULL; #define BIGMOD 1000012177LL #define INF 10000000000000LL void print_vec(const vector<LL> &v) { for (LL x : v) { printf("%lld ", x); } } bool isValid(const vector<LL> &maxes, const vector<LL> &sol) { int n = maxes.size(); printf("maxes: "); print_vec(maxes); printf("\n"); printf("sol: "); print_vec(sol); printf("\n"); for (int substrLen=1; substrLen <= n; substrLen++) { LL mx = -INF; for (int i=0; i < sol.size()-substrLen+1; i++) { LL sum = 0; for (int j=0; j < substrLen; j++) { sum += sol[i+j]; } //printf("sum:%lld\n", sum); mx = max(mx, sum); } if (mx != maxes[substrLen-1]) { printf("solution differ at len %d, expected: %lld got: %lld\n\n", substrLen, maxes[substrLen-1], mx); return false; } } printf("ok\n\n"); return true; } vector<LL> solve(const vector<LL> &maxes) { vector<LL> answer; vector<LL> current; LL currentSum = 0; for (int L=1; L <= maxes.size(); L++) { // niezmiennik: (current.size() + 1 == L) and (current satisfies maxes[1..L-1]) LL m = maxes[L-1]; LL neededElem = m - currentSum; //printf("L=%d m=%lld, neededElem=%lld\n", L, m, neededElem); current.emplace_back(neededElem); currentSum += neededElem; // Now, current.size() == L // Now, verify that neededElem keeps invariant int currentLen = current.size(); LL sum = 0; for (int i=1; i <= L; i++) { sum += current[currentLen-i]; if (sum > maxes[i-1]) { //printf("sum %lld > maxes[%d]=%lld at length %d\n", sum, i, maxes[i-1], L); return vector<LL>(); } } answer.insert(answer.end(), current.begin(), current.end()); answer.emplace_back(-INF); } return answer; } bool testSolve(const vector<LL> &maxes) { return isValid(maxes, solve(maxes)); } void selftest() { //isValid({3,4,5,-1}, {2, 2, -7, 0, 3, -7, 3, -1, 3}); //isValid({3,4,5,-1}, {3,-100,3,1,-100,3,1,1,-100,3,1,1,-6}); testSolve({3,4,5,-1}); //isValid({3,1,4,1,5}, {3, -INF, 3, -2, -INF, 3, -2, 3, -INF, 3, -2, 3, -3, -INF, 3, -2, 3, -3, 3}); testSolve({3,1,4,1,5}); testSolve({1,2,3,4,5,6,7,8,9,-1,-2}); testSolve({9,2,3,4,5,6,7,8,9,-1,-2}); testSolve({9,2,3,4,5,6,7,8,9,-1,-2}); //FOR(a,-3,3) FOR(b,-3,3) FOR(c, -3,3) FOR(d, -3,3) // testSolve({a,b,c,d}); //isValid({3,2,-3,2}, {3,-INF,3,-1,-INF,3,-1,-5,-INF,0,0,0,2}); FOR(a,0,3) FOR(b,0,3) FOR(c, 0,3) FOR(d, 0,3) testSolve({a,b,c,d}); //isValid({2,0,2,1}, {2,-INF,0,0,2,-1}); } int main() { //selftest(); int n; scanf("%d", &n); vector<LL> maxes; REP(i, n) { LL x; scanf("%lld", &x); maxes.emplace_back(x); } auto answer = solve(maxes); if (answer.size() == 0) { printf("NIE\n"); } else { printf("TAK\n"); printf("%d\n", answer.size()); for (LL x : answer) { printf("%lld ", x); } printf("\n"); } return 0; } /* NIE: 1 2 1 3 2 0 3 3 2 0 2 2 2 0 0 1 1 3 0 0 1 0 0 1 1 0 0 2 */ |