#include <bits/stdc++.h> using namespace std; int n, k, p; int t[500001]; int mmin[500001]; int mmax[500001]; vector <int> w; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> t[i]; } if (k > 3) { for (int i = 2; i <= n; i++) { if (t[i] <= t[i - 1]) p = i; } if (p == 0) { cout << "NIE" << endl; return 0; } k -= 3; for (int i = 1; i < p - 1; i++) { if (k == 0) break; w.push_back(i); k--; } w.push_back(p - 1); w.push_back(p); for (int i = p + 1; i <= n; i++) { if (k == 0) break; w.push_back(i); k--; } if (p == n) { w.pop_back(); w.pop_back(); w.push_back(n - 2); w.push_back(n - 1); } cout << "TAK" << endl; for (auto i : w) cout << i << " "; return 0; } mmin[1] = t[1]; for (int i = 2; i <= n; i++) { mmin[i] = min(mmin[i - 1], t[i]); } mmax[n] = t[n]; for (int i = n - 1; i >= 1; i--) { mmax[i] = max(mmax[i + 1], t[i]); } if (k == 3) { if (t[1] != mmin[n]) { for (int i = 1; i <= n; i++) { if (t[i] == mmin[n]) { cout << "TAK" << endl; cout << i - 1 << " " << i << endl; return 0; } } } if (t[n] != mmax[1]) { for (int i = n; i >= 1; i--) { if (t[i] == mmax[1]) { cout << "TAK" << endl; cout << i - 1 << " " << i << endl; return 0; } } } cout << "NIE" << endl; return 0; } if (k == 2) { for (int i = 1; i < n; i++) { if (mmin[i] >= mmax[i + 1]) { cout << "TAK" << endl << i << endl; return 0; } } cout << "NIE" << endl; } }
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 | #include <bits/stdc++.h> using namespace std; int n, k, p; int t[500001]; int mmin[500001]; int mmax[500001]; vector <int> w; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> t[i]; } if (k > 3) { for (int i = 2; i <= n; i++) { if (t[i] <= t[i - 1]) p = i; } if (p == 0) { cout << "NIE" << endl; return 0; } k -= 3; for (int i = 1; i < p - 1; i++) { if (k == 0) break; w.push_back(i); k--; } w.push_back(p - 1); w.push_back(p); for (int i = p + 1; i <= n; i++) { if (k == 0) break; w.push_back(i); k--; } if (p == n) { w.pop_back(); w.pop_back(); w.push_back(n - 2); w.push_back(n - 1); } cout << "TAK" << endl; for (auto i : w) cout << i << " "; return 0; } mmin[1] = t[1]; for (int i = 2; i <= n; i++) { mmin[i] = min(mmin[i - 1], t[i]); } mmax[n] = t[n]; for (int i = n - 1; i >= 1; i--) { mmax[i] = max(mmax[i + 1], t[i]); } if (k == 3) { if (t[1] != mmin[n]) { for (int i = 1; i <= n; i++) { if (t[i] == mmin[n]) { cout << "TAK" << endl; cout << i - 1 << " " << i << endl; return 0; } } } if (t[n] != mmax[1]) { for (int i = n; i >= 1; i--) { if (t[i] == mmax[1]) { cout << "TAK" << endl; cout << i - 1 << " " << i << endl; return 0; } } } cout << "NIE" << endl; return 0; } if (k == 2) { for (int i = 1; i < n; i++) { if (mmin[i] >= mmax[i + 1]) { cout << "TAK" << endl << i << endl; return 0; } } cout << "NIE" << endl; } } |