#define _CRT_SECURE_NO_WARNINGS #define ONLINE_JUDGE #include <iostream> #include <cstdio> #include <vector> #include <cassert> using namespace std; vector<int> readInput() { int numbers, temp; scanf("%d", &numbers); vector<int> result(numbers, 0); for (int i = 0; i < numbers; ++i) { scanf("%d", &temp); result[i] = temp; } return result; } class Solver { public: static vector<int> solve(const vector<int>& input) { vector<int> emptySet; vector<int> result; result.push_back(input[0]); for (int index = 1; index < input.size(); ++index) { result.push_back(input[index] - input[index - 1]); for (int smallerIndex = index - 1; smallerIndex >= 0; --smallerIndex) { const int lowestPossibleDifference = input[index] - input[smallerIndex]; const int maximumPossibleDifference = input[index - smallerIndex - 1]; if (lowestPossibleDifference > maximumPossibleDifference) return emptySet; } } return result; } }; void printResult(const vector<int> &result) { if (result.size() == 0) { printf("NIE\n"); return; } printf("TAK\n%d\n%d", result.size(), result[0]); for (int i = 1; i < result.size(); ++i) printf(" %d", result[i]); printf("\n"); } void assertEquals(vector<int> v1, vector<int> v2) { assert(v1.size() == v2.size()); for (int i = 0; i < v1.size(); ++i) assert(v1[i] == v2[i]); } void runTest1() { vector<int> input{ 4 }; vector<int> result = Solver::solve(input); assertEquals(result, input); input = vector<int>{ -333 }; result = Solver::solve(input); assertEquals(result, input); } void runTest2() { vector<int> input(2), result; for (int first = -40; first <= 40; ++first) { for (int second = -40; second <= 40; ++second) { input[0] = first, input[1] = second; if (input[1] > 2 * input[0]) { vector<int> expectedAnswer; result = Solver::solve(input); assertEquals(result, expectedAnswer); } else { vector<int> expectedAnswer{ input[0], input[1] - input[0] }; result = Solver::solve(input); assertEquals(result, expectedAnswer); } } } } void runTests() { runTest1(); runTest2(); } int main() { #ifndef ONLINE_JUDGE runTests(); printf("Tests finished.\n"); #endif vector<int> input = readInput(); vector<int> result = Solver::solve(input); printResult(result); #ifndef ONLINE_JUDGE system("pause"); #endif 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 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 | #define _CRT_SECURE_NO_WARNINGS #define ONLINE_JUDGE #include <iostream> #include <cstdio> #include <vector> #include <cassert> using namespace std; vector<int> readInput() { int numbers, temp; scanf("%d", &numbers); vector<int> result(numbers, 0); for (int i = 0; i < numbers; ++i) { scanf("%d", &temp); result[i] = temp; } return result; } class Solver { public: static vector<int> solve(const vector<int>& input) { vector<int> emptySet; vector<int> result; result.push_back(input[0]); for (int index = 1; index < input.size(); ++index) { result.push_back(input[index] - input[index - 1]); for (int smallerIndex = index - 1; smallerIndex >= 0; --smallerIndex) { const int lowestPossibleDifference = input[index] - input[smallerIndex]; const int maximumPossibleDifference = input[index - smallerIndex - 1]; if (lowestPossibleDifference > maximumPossibleDifference) return emptySet; } } return result; } }; void printResult(const vector<int> &result) { if (result.size() == 0) { printf("NIE\n"); return; } printf("TAK\n%d\n%d", result.size(), result[0]); for (int i = 1; i < result.size(); ++i) printf(" %d", result[i]); printf("\n"); } void assertEquals(vector<int> v1, vector<int> v2) { assert(v1.size() == v2.size()); for (int i = 0; i < v1.size(); ++i) assert(v1[i] == v2[i]); } void runTest1() { vector<int> input{ 4 }; vector<int> result = Solver::solve(input); assertEquals(result, input); input = vector<int>{ -333 }; result = Solver::solve(input); assertEquals(result, input); } void runTest2() { vector<int> input(2), result; for (int first = -40; first <= 40; ++first) { for (int second = -40; second <= 40; ++second) { input[0] = first, input[1] = second; if (input[1] > 2 * input[0]) { vector<int> expectedAnswer; result = Solver::solve(input); assertEquals(result, expectedAnswer); } else { vector<int> expectedAnswer{ input[0], input[1] - input[0] }; result = Solver::solve(input); assertEquals(result, expectedAnswer); } } } } void runTests() { runTest1(); runTest2(); } int main() { #ifndef ONLINE_JUDGE runTests(); printf("Tests finished.\n"); #endif vector<int> input = readInput(); vector<int> result = Solver::solve(input); printResult(result); #ifndef ONLINE_JUDGE system("pause"); #endif return 0; } |