#include <iostream> #include <vector> #include <set> #include <limits> using namespace std; void solve_k2(vector<int>& V) { int n = V.size(); vector<int> minL(n); vector<int> maxR(n); minL[0] = V[0]; maxR[n-1] = V[n-1]; for (int i = 1; i < n; i++) minL[i] = min(minL[i-1], V[i]); for (int i = n - 2; i >= 0; i--) maxR[i] = max(maxR[i+1], V[i]); for (int i = 0; i < n - 1; i++) { if (minL[i] >= maxR[i+1]) { cout << "TAK\n"; cout << i + 1 << "\n"; return; } } cout << "NIE\n"; } void solve_k3(vector<int>& V) { int n = V.size(); int mn = numeric_limits<int>::max(), mx = numeric_limits<int>::min(); for (int i = 0; i < n; i++) { mn = min(mn, V[i]); mx = max(mx, V[i]); } for (int i = 1; i < n; i++) if (V[i] == mn) { cout << "TAK\n"; if (i == n - 1) cout << i - 1 << " " << i << "\n"; else cout << i << " " << i + 1 << "\n"; return; } for (int i = 0; i < n - 1; i++) if (V[i] == mx) { cout << "TAK\n"; cout << i << " " << i + 1 << "\n"; return; } cout << "NIE\n"; } void solve_kbig(vector<int>& V, int k) { int n = V.size(); // find the pair int pair_pos; for (pair_pos = 0; pair_pos + 1 < n; pair_pos++) if (V[pair_pos] >= V[pair_pos + 1]) break; if (pair_pos + 1 == n) { cout << "NIE\n"; return; } // insert initial ranges set<int>Result; if (pair_pos != 0) Result.insert(pair_pos - 1); Result.insert(pair_pos); Result.insert(pair_pos + 1); // insert the rest arbitiary for (int i = n - 1; (int)Result.size() < k; i--) Result.insert(i); Result.erase(prev(Result.end())); // output cout << "TAK\n"; for (int x : Result) cout << x + 1 << " "; cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<int>A(n); for (int &x : A) cin >> x; if (k == 2) solve_k2(A); else if (k == 3) solve_k3(A); else solve_kbig(A, k); 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 | #include <iostream> #include <vector> #include <set> #include <limits> using namespace std; void solve_k2(vector<int>& V) { int n = V.size(); vector<int> minL(n); vector<int> maxR(n); minL[0] = V[0]; maxR[n-1] = V[n-1]; for (int i = 1; i < n; i++) minL[i] = min(minL[i-1], V[i]); for (int i = n - 2; i >= 0; i--) maxR[i] = max(maxR[i+1], V[i]); for (int i = 0; i < n - 1; i++) { if (minL[i] >= maxR[i+1]) { cout << "TAK\n"; cout << i + 1 << "\n"; return; } } cout << "NIE\n"; } void solve_k3(vector<int>& V) { int n = V.size(); int mn = numeric_limits<int>::max(), mx = numeric_limits<int>::min(); for (int i = 0; i < n; i++) { mn = min(mn, V[i]); mx = max(mx, V[i]); } for (int i = 1; i < n; i++) if (V[i] == mn) { cout << "TAK\n"; if (i == n - 1) cout << i - 1 << " " << i << "\n"; else cout << i << " " << i + 1 << "\n"; return; } for (int i = 0; i < n - 1; i++) if (V[i] == mx) { cout << "TAK\n"; cout << i << " " << i + 1 << "\n"; return; } cout << "NIE\n"; } void solve_kbig(vector<int>& V, int k) { int n = V.size(); // find the pair int pair_pos; for (pair_pos = 0; pair_pos + 1 < n; pair_pos++) if (V[pair_pos] >= V[pair_pos + 1]) break; if (pair_pos + 1 == n) { cout << "NIE\n"; return; } // insert initial ranges set<int>Result; if (pair_pos != 0) Result.insert(pair_pos - 1); Result.insert(pair_pos); Result.insert(pair_pos + 1); // insert the rest arbitiary for (int i = n - 1; (int)Result.size() < k; i--) Result.insert(i); Result.erase(prev(Result.end())); // output cout << "TAK\n"; for (int x : Result) cout << x + 1 << " "; cout << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<int>A(n); for (int &x : A) cin >> x; if (k == 2) solve_k2(A); else if (k == 3) solve_k3(A); else solve_kbig(A, k); return 0; } |