#include <iostream> #include <string> #include <vector> #include <algorithm> bool sprawdzCiag(const std::vector<int>& values) { bool canNextValueBeLargerThanZero = true; bool isAnyValuePositive = false; bool isFirstRemoved = false; bool isValid = true; int previous = 0; int current = 0; int it = 0; while (previous == 0 && it < values.size()) { previous = values[it]; it++; } for (int j = it; j < values.size(); j++) { current = values[j]; if(current > 0) { isAnyValuePositive = true; if(!canNextValueBeLargerThanZero) { isValid = false; } } else if(isAnyValuePositive) { canNextValueBeLargerThanZero = false; } if(!canNextValueBeLargerThanZero) { if(current > 0) { isValid = false; } } current = current - previous - 1; if(!isFirstRemoved) { if (current < 0) { current = current + 1; } else if(j + 1< values.size()){ if(values[j + 1] - 1 > current) { current = current + 1; } } } if (current < -1) { isValid = false; } if (current == -1) { canNextValueBeLargerThanZero = false; } isFirstRemoved = true; previous = current; } if (values.size() == 1 && previous == 1) { return true; } if(previous > 0){ isValid = false; } return isValid; } int main() { int tests; int n; std::cin >> tests; for (int i = 0; i < tests; i++) { std::cin >> n; std::vector<int> values(n); for (int j = 0; j < n; j++) { std::cin >> values[j]; } bool isValid = sprawdzCiag(values); /* std::reverse(values.begin(), values.end()); bool isValidReversed = sprawdzCiag(values);*/ if(isValid) { std::cout << "TAK" << std::endl; } else { std::cout << "NIE" << std::endl; } } 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 | #include <iostream> #include <string> #include <vector> #include <algorithm> bool sprawdzCiag(const std::vector<int>& values) { bool canNextValueBeLargerThanZero = true; bool isAnyValuePositive = false; bool isFirstRemoved = false; bool isValid = true; int previous = 0; int current = 0; int it = 0; while (previous == 0 && it < values.size()) { previous = values[it]; it++; } for (int j = it; j < values.size(); j++) { current = values[j]; if(current > 0) { isAnyValuePositive = true; if(!canNextValueBeLargerThanZero) { isValid = false; } } else if(isAnyValuePositive) { canNextValueBeLargerThanZero = false; } if(!canNextValueBeLargerThanZero) { if(current > 0) { isValid = false; } } current = current - previous - 1; if(!isFirstRemoved) { if (current < 0) { current = current + 1; } else if(j + 1< values.size()){ if(values[j + 1] - 1 > current) { current = current + 1; } } } if (current < -1) { isValid = false; } if (current == -1) { canNextValueBeLargerThanZero = false; } isFirstRemoved = true; previous = current; } if (values.size() == 1 && previous == 1) { return true; } if(previous > 0){ isValid = false; } return isValid; } int main() { int tests; int n; std::cin >> tests; for (int i = 0; i < tests; i++) { std::cin >> n; std::vector<int> values(n); for (int j = 0; j < n; j++) { std::cin >> values[j]; } bool isValid = sprawdzCiag(values); /* std::reverse(values.begin(), values.end()); bool isValidReversed = sprawdzCiag(values);*/ if(isValid) { std::cout << "TAK" << std::endl; } else { std::cout << "NIE" << std::endl; } } return 0; } |