#include <iostream> using namespace std; int* tab; int n, k; int INF = 1'000'000'001; int find_disorder() { // returns such an index "k" that tab[k] >= tab[k+1] for (int i = 1; i < n; i++) { if (tab[i - 1] >= tab[i]) { return i - 1; } } return -1; } int tab_min() { int result = INF; for (int i = 0; i < n; i++) if (result > tab[i]) result = tab[i]; return result; } int tab_max() { int result = -INF; for (int i = 0; i < n; i++) if (result < tab[i]) result = tab[i]; return result; } void solution(std::istream& instream, std::ostream& outstream) { instream >> n >> k; tab = new int[n]; for (int i=0; i < n; i++) instream >> tab[i]; if (k >= 4) { int disorder_start = find_disorder(); if (disorder_start == -1) { outstream << "NIE\n"; return; } outstream << "TAK\n"; int seq_start = std::min(disorder_start, n - k); for (int i = 1; i < k; i++) outstream << (i + seq_start) << " "; return; } if (k == 3) { int min = tab_min(), max = tab_max(); int i; for (i = 1; i < n - 1; i++) { if ((tab[i] == min) || (tab[i] == max)) { outstream << "TAK\n"; outstream << i << " " << i + 1; return; } } if ((tab[0] == max) && (tab[n - 1] == min)) { outstream << "TAK\n"; outstream << 1 << " " << n - 1; return; } outstream << "NIE\n"; return; } if (k == 2) { int* mins = new int[n]; int* maxs = new int[n]; mins[0] = tab[0]; for (int i = 1; i < n; i++) mins[i] = std::min(mins[i - 1], tab[i]); maxs[n - 1] = tab[n - 1]; for (int i = n - 2; i >= 0; i--) maxs[i] = std::max(maxs[i + 1], tab[i]); for (int i = 1; i < n; i++) if (mins[i - 1] >= maxs[i]) { outstream << "TAK\n"; outstream << i; return; } outstream << "NIE\n"; return; } } int main() { ios::sync_with_stdio(false); solution(std::cin, std::cout); }
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 | #include <iostream> using namespace std; int* tab; int n, k; int INF = 1'000'000'001; int find_disorder() { // returns such an index "k" that tab[k] >= tab[k+1] for (int i = 1; i < n; i++) { if (tab[i - 1] >= tab[i]) { return i - 1; } } return -1; } int tab_min() { int result = INF; for (int i = 0; i < n; i++) if (result > tab[i]) result = tab[i]; return result; } int tab_max() { int result = -INF; for (int i = 0; i < n; i++) if (result < tab[i]) result = tab[i]; return result; } void solution(std::istream& instream, std::ostream& outstream) { instream >> n >> k; tab = new int[n]; for (int i=0; i < n; i++) instream >> tab[i]; if (k >= 4) { int disorder_start = find_disorder(); if (disorder_start == -1) { outstream << "NIE\n"; return; } outstream << "TAK\n"; int seq_start = std::min(disorder_start, n - k); for (int i = 1; i < k; i++) outstream << (i + seq_start) << " "; return; } if (k == 3) { int min = tab_min(), max = tab_max(); int i; for (i = 1; i < n - 1; i++) { if ((tab[i] == min) || (tab[i] == max)) { outstream << "TAK\n"; outstream << i << " " << i + 1; return; } } if ((tab[0] == max) && (tab[n - 1] == min)) { outstream << "TAK\n"; outstream << 1 << " " << n - 1; return; } outstream << "NIE\n"; return; } if (k == 2) { int* mins = new int[n]; int* maxs = new int[n]; mins[0] = tab[0]; for (int i = 1; i < n; i++) mins[i] = std::min(mins[i - 1], tab[i]); maxs[n - 1] = tab[n - 1]; for (int i = n - 2; i >= 0; i--) maxs[i] = std::max(maxs[i + 1], tab[i]); for (int i = 1; i < n; i++) if (mins[i - 1] >= maxs[i]) { outstream << "TAK\n"; outstream << i; return; } outstream << "NIE\n"; return; } } int main() { ios::sync_with_stdio(false); solution(std::cin, std::cout); } |