#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; vi solve_len2(vi &a) { int n = a.size(); vi prefix_min(n); vi suffix_max(n); prefix_min[0] = a[0]; for (int i = 1; i < n; ++i) { prefix_min[i] = min(prefix_min[i - 1], a[i]); } suffix_max.back() = a.back(); for (int i = n - 2; i >= 0; --i) { suffix_max[i] = max(suffix_max[i + 1], a[i]); } for (int i = 1; i < n; ++i) { if (prefix_min[i - 1] >= suffix_max[i]) { return {i}; } } return {}; } vi solve_len3(vi &a) { int n = a.size(); int mn = 0, mx = 0; for (int i = 0; i < n; ++i) { if (a[i] <= a[mn]) { mn = i; } if (a[i] > a[mx]) { mx = i; } } if (mn == n - 1) { return {1, mn}; } if (mx == 0) { return {1, 2}; } if (mn != 0) { return {mn, mn + 1}; } if (mx != n - 1) { return {mx, mx + 1}; } return {}; } vi solve_len4(vi &a, int k) { int n = a.size(); int inversion_idx = -1; for (int i = 1; i < n; ++i) { if (a[i - 1] >= a[i]) { inversion_idx = i - 1; break; } } if (inversion_idx == -1) { return {}; } deque<int> result; result.push_back(inversion_idx + 1); if (inversion_idx + 2 < n) { result.push_back(inversion_idx + 2); } int i = inversion_idx - 1; while (i >= 0 && static_cast<int>(result.size()) < k - 1) { result.push_front(i + 1); --i; } i = inversion_idx + 2; while (i < n - 1 && static_cast<int>(result.size()) < k - 1) { result.push_back(i + 1); ++i; } return vi(result.begin(), result.end()); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vi a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vi result; if (k == 2) { result = solve_len2(a); } else if (k == 3) { result = solve_len3(a); } else { result = solve_len4(a, k); } if (result.size() > 0) { cout << "TAK\n"; for (size_t i = 0; i < result.size(); ++i) { cout << result[i] << ' '; } cout << '\n'; } else { cout << "NIE\n"; } 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long> vll; const ll LINF = 1e18; const int INF = 1e9; vi solve_len2(vi &a) { int n = a.size(); vi prefix_min(n); vi suffix_max(n); prefix_min[0] = a[0]; for (int i = 1; i < n; ++i) { prefix_min[i] = min(prefix_min[i - 1], a[i]); } suffix_max.back() = a.back(); for (int i = n - 2; i >= 0; --i) { suffix_max[i] = max(suffix_max[i + 1], a[i]); } for (int i = 1; i < n; ++i) { if (prefix_min[i - 1] >= suffix_max[i]) { return {i}; } } return {}; } vi solve_len3(vi &a) { int n = a.size(); int mn = 0, mx = 0; for (int i = 0; i < n; ++i) { if (a[i] <= a[mn]) { mn = i; } if (a[i] > a[mx]) { mx = i; } } if (mn == n - 1) { return {1, mn}; } if (mx == 0) { return {1, 2}; } if (mn != 0) { return {mn, mn + 1}; } if (mx != n - 1) { return {mx, mx + 1}; } return {}; } vi solve_len4(vi &a, int k) { int n = a.size(); int inversion_idx = -1; for (int i = 1; i < n; ++i) { if (a[i - 1] >= a[i]) { inversion_idx = i - 1; break; } } if (inversion_idx == -1) { return {}; } deque<int> result; result.push_back(inversion_idx + 1); if (inversion_idx + 2 < n) { result.push_back(inversion_idx + 2); } int i = inversion_idx - 1; while (i >= 0 && static_cast<int>(result.size()) < k - 1) { result.push_front(i + 1); --i; } i = inversion_idx + 2; while (i < n - 1 && static_cast<int>(result.size()) < k - 1) { result.push_back(i + 1); ++i; } return vi(result.begin(), result.end()); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vi a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vi result; if (k == 2) { result = solve_len2(a); } else if (k == 3) { result = solve_len3(a); } else { result = solve_len4(a, k); } if (result.size() > 0) { cout << "TAK\n"; for (size_t i = 0; i < result.size(); ++i) { cout << result[i] << ' '; } cout << '\n'; } else { cout << "NIE\n"; } return 0; } |