#include <bits/stdc++.h> using namespace std; vector <int> solve(int n, int k, vector <int> &a) { if (k == 2) { vector <int> minPref = a, maxSuff = a; for (int i = 1; i < n; i++) { minPref[i] = min(minPref[i], minPref[i - 1]); } for (int i = n - 2; i >= 0; i--) { maxSuff[i] = max(maxSuff[i], maxSuff[i + 1]); } for (int i = 0; i < n - 1; i++) if (minPref[i] >= maxSuff[i + 1]) { return {i}; } return {}; } else if (k == 3) { int minIdx = n - 1; for (int i = n - 2; i >= 0; i--) if (a[i] < a[minIdx]) { minIdx = i; } if (minIdx > 0) { if (minIdx < n - 1) { return {minIdx - 1, minIdx}; } else { return {n - 3, n - 2}; } } int maxIdx = 0; for (int i = 1; i < n; i++) if (a[i] > a[maxIdx]) { maxIdx = i; } if (maxIdx < n - 1) { if (maxIdx > 0) { return {maxIdx - 1, maxIdx}; } else { return {0, 1}; } } return {}; } else { int decrIdx = -1; for (int i = 0; i < n - 1; i++) if (a[i] >= a[i + 1]) { decrIdx = i; break; } if (decrIdx == -1) { return {}; } vector <bool> hasEnd(n, false); int numEnds = 2; hasEnd[decrIdx] = hasEnd[decrIdx + 1] = true; if (decrIdx > 0) { hasEnd[decrIdx - 1] = true; numEnds++; } if (!hasEnd[n - 1]) { hasEnd[n - 1] = true; numEnds++; } for (int i = 0; i < n && numEnds < k; i++) { if (!hasEnd[i]) { hasEnd[i] = true; numEnds++; } } vector <int> ans; for (int i = 0; i < n - 1; i++) if (hasEnd[i]) { ans.push_back(i); } return ans; } } int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; vector <int> a(n); for (int &ai : a) { cin >> ai; } auto ans = solve(n, k, a); if (ans.empty()) { cout << "NIE"; } else { cout << "TAK\n"; for (int i : ans) { cout << i + 1 << ' '; } } 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 | #include <bits/stdc++.h> using namespace std; vector <int> solve(int n, int k, vector <int> &a) { if (k == 2) { vector <int> minPref = a, maxSuff = a; for (int i = 1; i < n; i++) { minPref[i] = min(minPref[i], minPref[i - 1]); } for (int i = n - 2; i >= 0; i--) { maxSuff[i] = max(maxSuff[i], maxSuff[i + 1]); } for (int i = 0; i < n - 1; i++) if (minPref[i] >= maxSuff[i + 1]) { return {i}; } return {}; } else if (k == 3) { int minIdx = n - 1; for (int i = n - 2; i >= 0; i--) if (a[i] < a[minIdx]) { minIdx = i; } if (minIdx > 0) { if (minIdx < n - 1) { return {minIdx - 1, minIdx}; } else { return {n - 3, n - 2}; } } int maxIdx = 0; for (int i = 1; i < n; i++) if (a[i] > a[maxIdx]) { maxIdx = i; } if (maxIdx < n - 1) { if (maxIdx > 0) { return {maxIdx - 1, maxIdx}; } else { return {0, 1}; } } return {}; } else { int decrIdx = -1; for (int i = 0; i < n - 1; i++) if (a[i] >= a[i + 1]) { decrIdx = i; break; } if (decrIdx == -1) { return {}; } vector <bool> hasEnd(n, false); int numEnds = 2; hasEnd[decrIdx] = hasEnd[decrIdx + 1] = true; if (decrIdx > 0) { hasEnd[decrIdx - 1] = true; numEnds++; } if (!hasEnd[n - 1]) { hasEnd[n - 1] = true; numEnds++; } for (int i = 0; i < n && numEnds < k; i++) { if (!hasEnd[i]) { hasEnd[i] = true; numEnds++; } } vector <int> ans; for (int i = 0; i < n - 1; i++) if (hasEnd[i]) { ans.push_back(i); } return ans; } } int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; vector <int> a(n); for (int &ai : a) { cin >> ai; } auto ans = solve(n, k, a); if (ans.empty()) { cout << "NIE"; } else { cout << "TAK\n"; for (int i : ans) { cout << i + 1 << ' '; } } return 0; } |