#include <iostream> #include <vector> #include <cassert> #include <climits> using namespace std; bool correct(vector<int> indices, vector<int> v, int k) { if (indices.size() != k) { return false; } indices.push_back((int)v.size()); for (int i = 1; i < indices.size(); ++i) { if (indices[i] <= indices[i - 1]) { return false; } } for (int i = 0; i < indices.size() - 1; ++i) { int fl = (i == 0 ? 0 : indices[i - 1]); int fr = indices[i] - 1; int sl = indices[i]; int sr = indices[i + 1] - 1; int minl = INT_MIN; int maxr = INT_MAX; for (int j = fl; j <= fr; ++j) { minl = min(minl, v[j]); } for (int j = sl; j <= sr; ++j) { maxr = max(maxr, v[j]); } if (minl >= maxr) { return false; } } return true; } int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int &i : v) { cin >> i; } vector<int> max_end(n), min_beg(n); min_beg[0] = v[0]; max_end[n - 1] = v[n - 1]; for (int i = 1; i < n; ++i) { min_beg[i] = min(v[i], min_beg[i - 1]); } for (int i = n - 2; i >= 0; --i) { max_end[i] = max(v[i], max_end[i + 1]); } if (k == 2) { bool possible = false; int index; for (int i = 0; i < n - 1; ++i) { if (min_beg[i] >= max_end[i + 1]) { possible = true; index = i + 1; break; } } if (possible) { cout << "TAK\n"; cout << index; } else { cout << "NIE\n"; } } else { bool possible = false; int index; for (int i = 1; i < n - 1; ++i) { if (v[i] <= min_beg[i - 1] || v[i] >= max_end[i + 1]) { possible = true; index = i + 1; break; } } if (k == 3) { if (possible) { cout << "TAK\n"; cout << index - 1 << ' ' << index; } else { cout << "NIE\n"; } return 0; } if (k > 3) { for (int i = 1; i < n - 1; ++i) { if (v[i] <= v[i - 1]) { possible = true; index = i + 1; break; } } } if (possible) { cout << "TAK\n"; vector<int> indices; int kk = k - 1; k -= 2; --k; for (int i = 1; i < index - 1 && k > 0; ++i, --k) { indices.push_back(i); } indices.push_back(index - 1); indices.push_back(index); for (int i = index + 1; i <= n && k > 0; ++i, --k) { indices.push_back(i); } // assert(correct(indices, v, kk)); cout << indices[0]; for (int i = 1; i < indices.size(); ++i) { cout << ' ' << indices[i]; } } 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 135 136 137 138 | #include <iostream> #include <vector> #include <cassert> #include <climits> using namespace std; bool correct(vector<int> indices, vector<int> v, int k) { if (indices.size() != k) { return false; } indices.push_back((int)v.size()); for (int i = 1; i < indices.size(); ++i) { if (indices[i] <= indices[i - 1]) { return false; } } for (int i = 0; i < indices.size() - 1; ++i) { int fl = (i == 0 ? 0 : indices[i - 1]); int fr = indices[i] - 1; int sl = indices[i]; int sr = indices[i + 1] - 1; int minl = INT_MIN; int maxr = INT_MAX; for (int j = fl; j <= fr; ++j) { minl = min(minl, v[j]); } for (int j = sl; j <= sr; ++j) { maxr = max(maxr, v[j]); } if (minl >= maxr) { return false; } } return true; } int main() { int n, k; cin >> n >> k; vector<int> v(n); for (int &i : v) { cin >> i; } vector<int> max_end(n), min_beg(n); min_beg[0] = v[0]; max_end[n - 1] = v[n - 1]; for (int i = 1; i < n; ++i) { min_beg[i] = min(v[i], min_beg[i - 1]); } for (int i = n - 2; i >= 0; --i) { max_end[i] = max(v[i], max_end[i + 1]); } if (k == 2) { bool possible = false; int index; for (int i = 0; i < n - 1; ++i) { if (min_beg[i] >= max_end[i + 1]) { possible = true; index = i + 1; break; } } if (possible) { cout << "TAK\n"; cout << index; } else { cout << "NIE\n"; } } else { bool possible = false; int index; for (int i = 1; i < n - 1; ++i) { if (v[i] <= min_beg[i - 1] || v[i] >= max_end[i + 1]) { possible = true; index = i + 1; break; } } if (k == 3) { if (possible) { cout << "TAK\n"; cout << index - 1 << ' ' << index; } else { cout << "NIE\n"; } return 0; } if (k > 3) { for (int i = 1; i < n - 1; ++i) { if (v[i] <= v[i - 1]) { possible = true; index = i + 1; break; } } } if (possible) { cout << "TAK\n"; vector<int> indices; int kk = k - 1; k -= 2; --k; for (int i = 1; i < index - 1 && k > 0; ++i, --k) { indices.push_back(i); } indices.push_back(index - 1); indices.push_back(index); for (int i = index + 1; i <= n && k > 0; ++i, --k) { indices.push_back(i); } // assert(correct(indices, v, kk)); cout << indices[0]; for (int i = 1; i < indices.size(); ++i) { cout << ' ' << indices[i]; } } else { cout << "NIE\n"; } } return 0; } |