#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int n, k; int tab[N]; int mn[N], mx[N]; int check_asc() { for (int i = 1; i < n; i++) { if (tab[i - 1] >= tab[i]) { return i; } } return -1; } int check_min() { for (int i = 1; i < n; i++) { if (tab[0] >= tab[i]) { return i; } } return -1; } int check_max() { for (int i = n - 2; i >= 0; i--) { if (tab[i] >= tab[n - 1]) { return i; } } return -1; } void brute() { mn[0] = tab[0]; for (int i = 1; i < n; i++) { mn[i] = min(mn[i - 1], tab[i]); } mx[n - 1] = tab[n - 1]; for (int i = n - 2; i >= 0; i--) { mx[i] = max(mx[i + 1], tab[i]); } for (int i = 0; i < n - 1; i++) { if (mn[i] >= mx[i + 1]) { cout << "TAK\n"; cout << i + 1 << "\n"; return; } } cout << "NIE\n"; } void solve() { int ind = check_asc(); if (ind == -1) { cout << "NIE\n"; return; } if (k >= 4) { cout << "TAK\n"; if (ind == 1) { for (int i = 1; i < k; i++) { cout << i << " "; } cout << "\n"; } else if (ind == n - 1) { for (int i = n - k + 1; i < n; i++) { cout << i << " "; } cout << "\n"; } else { int cnt = k - 4; for (int i = 0; i < ind - 2 && cnt > 0; i++) { cout << i + 1 << " "; cnt--; } for (int i = -1; i < 2; i++) { cout << ind + i << " "; } for (int i = ind + 1; i < n && cnt > 0; i++) { cout << i + 1 << " "; cnt--; } cout << "\n"; } return; } if (k == 2) { brute(); return; } // k == 3 ind = check_min(); if (ind != -1) { cout << "TAK\n"; if (ind == n - 1) { cout << 1 << " " << n - 1 << "\n"; } else { cout << ind << " " << ind + 1 << "\n"; } return; } ind = check_max(); if (ind != -1) { cout << "TAK\n"; cout << ind << " " << ind + 1 << "\n"; return; } cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tab[i]; } solve(); }
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 | #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int n, k; int tab[N]; int mn[N], mx[N]; int check_asc() { for (int i = 1; i < n; i++) { if (tab[i - 1] >= tab[i]) { return i; } } return -1; } int check_min() { for (int i = 1; i < n; i++) { if (tab[0] >= tab[i]) { return i; } } return -1; } int check_max() { for (int i = n - 2; i >= 0; i--) { if (tab[i] >= tab[n - 1]) { return i; } } return -1; } void brute() { mn[0] = tab[0]; for (int i = 1; i < n; i++) { mn[i] = min(mn[i - 1], tab[i]); } mx[n - 1] = tab[n - 1]; for (int i = n - 2; i >= 0; i--) { mx[i] = max(mx[i + 1], tab[i]); } for (int i = 0; i < n - 1; i++) { if (mn[i] >= mx[i + 1]) { cout << "TAK\n"; cout << i + 1 << "\n"; return; } } cout << "NIE\n"; } void solve() { int ind = check_asc(); if (ind == -1) { cout << "NIE\n"; return; } if (k >= 4) { cout << "TAK\n"; if (ind == 1) { for (int i = 1; i < k; i++) { cout << i << " "; } cout << "\n"; } else if (ind == n - 1) { for (int i = n - k + 1; i < n; i++) { cout << i << " "; } cout << "\n"; } else { int cnt = k - 4; for (int i = 0; i < ind - 2 && cnt > 0; i++) { cout << i + 1 << " "; cnt--; } for (int i = -1; i < 2; i++) { cout << ind + i << " "; } for (int i = ind + 1; i < n && cnt > 0; i++) { cout << i + 1 << " "; cnt--; } cout << "\n"; } return; } if (k == 2) { brute(); return; } // k == 3 ind = check_min(); if (ind != -1) { cout << "TAK\n"; if (ind == n - 1) { cout << 1 << " " << n - 1 << "\n"; } else { cout << ind << " " << ind + 1 << "\n"; } return; } ind = check_max(); if (ind != -1) { cout << "TAK\n"; cout << ind << " " << ind + 1 << "\n"; return; } cout << "NIE\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tab[i]; } solve(); } |