#include <iostream> #include <vector> #include <set> using namespace std; /* 10 3 1 2 3 4 6 5 7 8 9 10 10 3 1 2 3 4 5 5 7 8 9 10 10 4 1 2 3 4 6 5 7 8 9 10 10 4 1 2 3 4 5 5 7 8 9 10 * */ int findLastMinElementIndex(std::vector<int> arr) { int minIndex = 0; for (int i = 1; i < arr.size(); i++) { if(arr[i] <= arr[minIndex]) { minIndex = i; } } return minIndex; } int findFirstMaxElementIndex(std::vector<int> arr) { int maxIndex = 0; for (int i = 1; i < arr.size(); i++) { if(arr[i] > arr[maxIndex]) { maxIndex = i; } } return maxIndex; } int checkIfIsStrictlyGrowing(std::vector<int> arr) { for (int i = 0; i < arr.size() - 1; i++) { if (arr[i] >= arr[i + 1]) { return i; } } return -1; } void solveForK2(std::vector<int> arr) { if(arr.size() == 2) { if(arr[0] >= arr[1]) { printf("TAK\n"); printf("1\n"); //+1 because elements in assignment are indexed starting at 1 } else { printf("NIE\n"); //should never happen } } else { int smallestToToLeft[arr.size()]; int biggestToTheRight[arr.size()]; smallestToToLeft[0] = arr[0]; for (int i = 1; i < arr.size(); i++) { smallestToToLeft[i] = min(smallestToToLeft[i - 1], arr[i]); } biggestToTheRight[arr.size() - 2] = arr[arr.size() - 1]; for (int i = arr.size() - 3; i > 0; i--) { biggestToTheRight[i] = max(biggestToTheRight[i + 1], arr[i - 1]); } biggestToTheRight[0] = biggestToTheRight[1]; bool done = false; for(int i = 0; i < arr.size() - 2 && !done; i++) { if(smallestToToLeft[i] >= biggestToTheRight[i]) { printf("TAK\n"); printf("%d\n", i + 1); //+1 because elements in assignment are indexed starting at 1 done = true; } } if(!done) { printf("NIE\n"); } } } void printResult(std::set<int> result, int k) { printf("TAK\n"); int elem = 0; while(result.size() < k - 1) { result.insert(elem); elem++; } for (int iter : result) { printf("%d ", iter + 1); //+1 because elements in assignment are indexed starting at 1 } printf("\n"); } int main() { int n, k, a; scanf("%d %d", &n, &k); std::vector<int> arr; for (int i = 0; i < n; i++) { scanf("%d", &a); arr.push_back(a); } int indexOfLastGrowingElement = checkIfIsStrictlyGrowing(arr); if (indexOfLastGrowingElement == -1) { printf("NIE\n"); } else { std::set<int> result; if (k >= 4) { result.insert(indexOfLastGrowingElement); result.insert(indexOfLastGrowingElement + 1); if (indexOfLastGrowingElement > 0) { result.insert(indexOfLastGrowingElement - 1); } printResult(result, k); } else if (k == 3) { int minElementIndex = findLastMinElementIndex(arr); if(minElementIndex != 0) { result.insert(minElementIndex); result.insert(minElementIndex - 1); printResult(result, k); } else { int maxElementIndex = findFirstMaxElementIndex(arr); if(maxElementIndex != arr.size() - 1) { result.insert(maxElementIndex); if(maxElementIndex > 0) { result.insert(maxElementIndex - 1); } printResult(result, k); } else { printf("NIE\n"); } } } else { //k == 2 solveForK2(arr); } } 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 128 129 130 131 132 133 134 135 136 137 138 139 140 | #include <iostream> #include <vector> #include <set> using namespace std; /* 10 3 1 2 3 4 6 5 7 8 9 10 10 3 1 2 3 4 5 5 7 8 9 10 10 4 1 2 3 4 6 5 7 8 9 10 10 4 1 2 3 4 5 5 7 8 9 10 * */ int findLastMinElementIndex(std::vector<int> arr) { int minIndex = 0; for (int i = 1; i < arr.size(); i++) { if(arr[i] <= arr[minIndex]) { minIndex = i; } } return minIndex; } int findFirstMaxElementIndex(std::vector<int> arr) { int maxIndex = 0; for (int i = 1; i < arr.size(); i++) { if(arr[i] > arr[maxIndex]) { maxIndex = i; } } return maxIndex; } int checkIfIsStrictlyGrowing(std::vector<int> arr) { for (int i = 0; i < arr.size() - 1; i++) { if (arr[i] >= arr[i + 1]) { return i; } } return -1; } void solveForK2(std::vector<int> arr) { if(arr.size() == 2) { if(arr[0] >= arr[1]) { printf("TAK\n"); printf("1\n"); //+1 because elements in assignment are indexed starting at 1 } else { printf("NIE\n"); //should never happen } } else { int smallestToToLeft[arr.size()]; int biggestToTheRight[arr.size()]; smallestToToLeft[0] = arr[0]; for (int i = 1; i < arr.size(); i++) { smallestToToLeft[i] = min(smallestToToLeft[i - 1], arr[i]); } biggestToTheRight[arr.size() - 2] = arr[arr.size() - 1]; for (int i = arr.size() - 3; i > 0; i--) { biggestToTheRight[i] = max(biggestToTheRight[i + 1], arr[i - 1]); } biggestToTheRight[0] = biggestToTheRight[1]; bool done = false; for(int i = 0; i < arr.size() - 2 && !done; i++) { if(smallestToToLeft[i] >= biggestToTheRight[i]) { printf("TAK\n"); printf("%d\n", i + 1); //+1 because elements in assignment are indexed starting at 1 done = true; } } if(!done) { printf("NIE\n"); } } } void printResult(std::set<int> result, int k) { printf("TAK\n"); int elem = 0; while(result.size() < k - 1) { result.insert(elem); elem++; } for (int iter : result) { printf("%d ", iter + 1); //+1 because elements in assignment are indexed starting at 1 } printf("\n"); } int main() { int n, k, a; scanf("%d %d", &n, &k); std::vector<int> arr; for (int i = 0; i < n; i++) { scanf("%d", &a); arr.push_back(a); } int indexOfLastGrowingElement = checkIfIsStrictlyGrowing(arr); if (indexOfLastGrowingElement == -1) { printf("NIE\n"); } else { std::set<int> result; if (k >= 4) { result.insert(indexOfLastGrowingElement); result.insert(indexOfLastGrowingElement + 1); if (indexOfLastGrowingElement > 0) { result.insert(indexOfLastGrowingElement - 1); } printResult(result, k); } else if (k == 3) { int minElementIndex = findLastMinElementIndex(arr); if(minElementIndex != 0) { result.insert(minElementIndex); result.insert(minElementIndex - 1); printResult(result, k); } else { int maxElementIndex = findFirstMaxElementIndex(arr); if(maxElementIndex != arr.size() - 1) { result.insert(maxElementIndex); if(maxElementIndex > 0) { result.insert(maxElementIndex - 1); } printResult(result, k); } else { printf("NIE\n"); } } } else { //k == 2 solveForK2(arr); } } return 0; } |