#include <iostream> using namespace std; int tab[500007]; int min_suf[500007]; int ile[500007]; int min_pref[500007]; bool cut[500007]; int licz = 0; // k >= 3 bool sprawdz(int pocz, int n) { int m = tab[pocz]; for (int i = pocz + 1; i < n; i++) { if (tab[i] <= m) { cut[i - 1] = true; cut[i] = true; licz += 2; return true; } } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tab[i]; } if (k == 2) { int pocz = 0; min_pref[pocz] = tab[pocz]; for (int i = pocz + 1; i < n; i++) { min_pref[i] = min(min_pref[i - 1], tab[i]); } int max_suf = tab[n - 1]; for (int i = n - 2; i >= pocz; i--) { if (max_suf <= min_pref[i]) { cout << "TAK\n"; cout << i + 1 << "\n"; return 0; } max_suf = max(max_suf, tab[i]); } cout << "NIE\n"; return 0; } // k >= 3 // licze jedyne globalne minima z lewej min_suf[n - 1] = tab[n - 1]; ile[n - 1] = 1; for (int i = n - 2; i >= 0; i--) { if (tab[i] < min_suf[i + 1]) { min_suf[i] = tab[i]; ile[i] = 1; } else if (tab[i] == min_suf[i + 1]) { min_suf[i] = min_suf[i + 1]; ile[i] = ile[i + 1] + 1; } else { min_suf[i] = min_suf[i + 1]; ile[i] = ile[i + 1]; } } int pocz = 0; while (tab[pocz] == min_suf[pocz] && ile[pocz] == 1) { pocz++; } if (pocz != 0) { if (k - 1 == 2) { int max_suf = tab[n - 1]; for (int i = n - 2; i >= pocz; i--) { if (max_suf <= tab[i]) { cout << "TAK\n"; cout << i << " " << i + 1 << "\n"; return 0; } max_suf = max(max_suf, tab[i]); } cout << "NIE\n"; return 0; } else { cut[pocz - 1] = true; licz++; } } if (!sprawdz(pocz, n)) { cout << "NIE\n"; } else { cout << "TAK\n"; for (int i = 0; i < n; i++) { if (cut[i] == false && licz < k - 1) { cut[i] = true; licz++; } if (cut[i] == true) { cout << i + 1 << " "; } } } // k >= 3 }
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 | #include <iostream> using namespace std; int tab[500007]; int min_suf[500007]; int ile[500007]; int min_pref[500007]; bool cut[500007]; int licz = 0; // k >= 3 bool sprawdz(int pocz, int n) { int m = tab[pocz]; for (int i = pocz + 1; i < n; i++) { if (tab[i] <= m) { cut[i - 1] = true; cut[i] = true; licz += 2; return true; } } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tab[i]; } if (k == 2) { int pocz = 0; min_pref[pocz] = tab[pocz]; for (int i = pocz + 1; i < n; i++) { min_pref[i] = min(min_pref[i - 1], tab[i]); } int max_suf = tab[n - 1]; for (int i = n - 2; i >= pocz; i--) { if (max_suf <= min_pref[i]) { cout << "TAK\n"; cout << i + 1 << "\n"; return 0; } max_suf = max(max_suf, tab[i]); } cout << "NIE\n"; return 0; } // k >= 3 // licze jedyne globalne minima z lewej min_suf[n - 1] = tab[n - 1]; ile[n - 1] = 1; for (int i = n - 2; i >= 0; i--) { if (tab[i] < min_suf[i + 1]) { min_suf[i] = tab[i]; ile[i] = 1; } else if (tab[i] == min_suf[i + 1]) { min_suf[i] = min_suf[i + 1]; ile[i] = ile[i + 1] + 1; } else { min_suf[i] = min_suf[i + 1]; ile[i] = ile[i + 1]; } } int pocz = 0; while (tab[pocz] == min_suf[pocz] && ile[pocz] == 1) { pocz++; } if (pocz != 0) { if (k - 1 == 2) { int max_suf = tab[n - 1]; for (int i = n - 2; i >= pocz; i--) { if (max_suf <= tab[i]) { cout << "TAK\n"; cout << i << " " << i + 1 << "\n"; return 0; } max_suf = max(max_suf, tab[i]); } cout << "NIE\n"; return 0; } else { cut[pocz - 1] = true; licz++; } } if (!sprawdz(pocz, n)) { cout << "NIE\n"; } else { cout << "TAK\n"; for (int i = 0; i < n; i++) { if (cut[i] == false && licz < k - 1) { cut[i] = true; licz++; } if (cut[i] == true) { cout << i + 1 << " "; } } } // k >= 3 } |