#include <iostream> #include <vector> #include <algorithm> void solve_for_2(int n, std::vector<int> const & v) { std::vector<int> pref_min(n); std::vector<int> suf_max(n); pref_min[0] = v[0]; suf_max[n - 1] = v[n - 1]; for (int i = 1; i < n; i++) pref_min[i] = std::min(v[i], pref_min[i - 1]); for (int i = n - 2; i >= 0; i--) { suf_max[i] = std::max(v[i], suf_max[i + 1]); } for (int i = 0; i < n - 1; i++) { if (pref_min[i] >= suf_max[i + 1]) { std::cout << "TAK\n"; std::cout << i + 1 << "\n"; return; } } std::cout << "NIE\n"; } void solve_for_3(int n, std::vector<int> const & v) { int min = *(std::min_element(v.begin(), v.end())); int max = *(std::max_element(v.begin(), v.end())); for (int i = 1; i < n - 1; i++) { if ((v[i] == min) || (v[i] == max)) { std::cout << "TAK\n"; std::cout << i << " " << i + 1 << "\n"; return; } } if (v[0] >= v[n - 1]) { std::cout << "TAK\n"; std::cout << 1 << " " << 2 << "\n"; return; } std::cout << "NIE\n"; } void solve(int n, std::vector<int> const & v, int k_left) { int curr = -1; for (int i = 0; i < n - 1; i++) { if (v[i] >= v[i + 1]) { curr = i; break; } } if (curr == -1) { std::cout << "NIE\n"; return; } if (curr > 0) k_left --; k_left --; if (curr < n - 2) k_left --; std::cout << "TAK\n"; for (int i = 0; i < n - 1; i++) { if ((i == (curr - 1)) || (i == curr) || (i == (curr + 1))) std::cout << i + 1 << " "; else if (k_left) { k_left--; std::cout << i + 1 << " "; } } std::cout << "\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cin.tie(NULL); int n, k; std::cin >> n >> k; std::vector<int> v(n); for (int i = 0; i < n; i++) std::cin >> v[i]; switch (k) { case 2: solve_for_2(n, v); break; case 3: solve_for_3(n, v); break; default: solve(n, v, k - 1); } }
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 | #include <iostream> #include <vector> #include <algorithm> void solve_for_2(int n, std::vector<int> const & v) { std::vector<int> pref_min(n); std::vector<int> suf_max(n); pref_min[0] = v[0]; suf_max[n - 1] = v[n - 1]; for (int i = 1; i < n; i++) pref_min[i] = std::min(v[i], pref_min[i - 1]); for (int i = n - 2; i >= 0; i--) { suf_max[i] = std::max(v[i], suf_max[i + 1]); } for (int i = 0; i < n - 1; i++) { if (pref_min[i] >= suf_max[i + 1]) { std::cout << "TAK\n"; std::cout << i + 1 << "\n"; return; } } std::cout << "NIE\n"; } void solve_for_3(int n, std::vector<int> const & v) { int min = *(std::min_element(v.begin(), v.end())); int max = *(std::max_element(v.begin(), v.end())); for (int i = 1; i < n - 1; i++) { if ((v[i] == min) || (v[i] == max)) { std::cout << "TAK\n"; std::cout << i << " " << i + 1 << "\n"; return; } } if (v[0] >= v[n - 1]) { std::cout << "TAK\n"; std::cout << 1 << " " << 2 << "\n"; return; } std::cout << "NIE\n"; } void solve(int n, std::vector<int> const & v, int k_left) { int curr = -1; for (int i = 0; i < n - 1; i++) { if (v[i] >= v[i + 1]) { curr = i; break; } } if (curr == -1) { std::cout << "NIE\n"; return; } if (curr > 0) k_left --; k_left --; if (curr < n - 2) k_left --; std::cout << "TAK\n"; for (int i = 0; i < n - 1; i++) { if ((i == (curr - 1)) || (i == curr) || (i == (curr + 1))) std::cout << i + 1 << " "; else if (k_left) { k_left--; std::cout << i + 1 << " "; } } std::cout << "\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cin.tie(NULL); int n, k; std::cin >> n >> k; std::vector<int> v(n); for (int i = 0; i < n; i++) std::cin >> v[i]; switch (k) { case 2: solve_for_2(n, v); break; case 3: solve_for_3(n, v); break; default: solve(n, v, k - 1); } } |