#include <iostream> #include <algorithm> int32_t n, k, a[500001], a_lmin[500001]; void solve_ge4() { int32_t ninc_idx = -1; for (int32_t i = 0; i < n - 1; ++i) { if (a[i] >= a[i+1]) { ninc_idx = i; break; } } if (ninc_idx == -1) { std::cout << "NIE\n"; return; } std::cout << "TAK\n"; // I'm lazy if (ninc_idx > 0) { a_lmin[ninc_idx-1] = 1; --k; } a_lmin[ninc_idx] = 1; --k; a_lmin[ninc_idx + 1] = 1; --k; if (ninc_idx + 2 < n) { a_lmin[ninc_idx+2] = 1; --k; } for (int32_t i = 0; i < n && k > 0; ++i) { if (a_lmin[i] == 0) { a_lmin[i] = 1; --k; } } for (int32_t i = 0; i < n; ++i) { if (a_lmin[i] == 1) { std::cout << i + 1 << " "; } } std::cout << "\n"; } void solve_3() { int32_t a_min = a[0], a_max = a[0], i_min = 0, i_max = 0; for (int32_t i = 1; i < n; ++i) { if (a[i] < a_min) { a_min = a[i]; i_min = i; } else if (a[i] == a_min) { i_min = i; } if (a[i] > a_max) { a_max = a[i]; i_max = i; } } if (i_min != 0) { std::cout << "TAK\n"; std::cout << i_min << " " << i_min + 1 << "\n"; return; } if (i_max != n - 1) { std::cout << "TAK\n"; std::cout << i_max << " " << i_max + 1 << "\n"; return; } std::cout << "NIE\n"; } void solve_2() { a_lmin[0] = a[0]; for (int32_t i = 1; i < n; ++i) { a_lmin[i] = std::min(a_lmin[i-1], a[i]); } int32_t a_rmax = a[n-1]; for (int32_t i = n - 2; i >= 0; --i) { if (a_lmin[i] >= a_rmax) { std::cout << "TAK\n"; std::cout << i + 1 << "\n"; return; } a_rmax = std::max(a_rmax, a[i]); } std::cout << "NIE\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin >> n >> k; for (int32_t i = 0; i < n; ++i) { std::cin >> a[i]; } if (k >= 4) { solve_ge4(); return 0; } else if (k == 3) { solve_3(); } else { solve_2(); } }
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 | #include <iostream> #include <algorithm> int32_t n, k, a[500001], a_lmin[500001]; void solve_ge4() { int32_t ninc_idx = -1; for (int32_t i = 0; i < n - 1; ++i) { if (a[i] >= a[i+1]) { ninc_idx = i; break; } } if (ninc_idx == -1) { std::cout << "NIE\n"; return; } std::cout << "TAK\n"; // I'm lazy if (ninc_idx > 0) { a_lmin[ninc_idx-1] = 1; --k; } a_lmin[ninc_idx] = 1; --k; a_lmin[ninc_idx + 1] = 1; --k; if (ninc_idx + 2 < n) { a_lmin[ninc_idx+2] = 1; --k; } for (int32_t i = 0; i < n && k > 0; ++i) { if (a_lmin[i] == 0) { a_lmin[i] = 1; --k; } } for (int32_t i = 0; i < n; ++i) { if (a_lmin[i] == 1) { std::cout << i + 1 << " "; } } std::cout << "\n"; } void solve_3() { int32_t a_min = a[0], a_max = a[0], i_min = 0, i_max = 0; for (int32_t i = 1; i < n; ++i) { if (a[i] < a_min) { a_min = a[i]; i_min = i; } else if (a[i] == a_min) { i_min = i; } if (a[i] > a_max) { a_max = a[i]; i_max = i; } } if (i_min != 0) { std::cout << "TAK\n"; std::cout << i_min << " " << i_min + 1 << "\n"; return; } if (i_max != n - 1) { std::cout << "TAK\n"; std::cout << i_max << " " << i_max + 1 << "\n"; return; } std::cout << "NIE\n"; } void solve_2() { a_lmin[0] = a[0]; for (int32_t i = 1; i < n; ++i) { a_lmin[i] = std::min(a_lmin[i-1], a[i]); } int32_t a_rmax = a[n-1]; for (int32_t i = n - 2; i >= 0; --i) { if (a_lmin[i] >= a_rmax) { std::cout << "TAK\n"; std::cout << i + 1 << "\n"; return; } a_rmax = std::max(a_rmax, a[i]); } std::cout << "NIE\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin >> n >> k; for (int32_t i = 0; i < n; ++i) { std::cin >> a[i]; } if (k >= 4) { solve_ge4(); return 0; } else if (k == 3) { solve_3(); } else { solve_2(); } } |