#include <iostream> void k4(bool *success, int n, int k, int *a, int *r) { int ind1 = 0; int ind2 = 0; for (int i = 0; i < n; i++) { if (a[i-1] >= a[i]) { ind1 = i-1; ind2 = i; break; } } if (ind1 != ind2) { *success = true; int p_left = k - 3; int ind = 0; for (int i = 0; i < ind1-1 && p_left > 0; i++) { r[ind] = i; ind++; p_left--; } r[ind] = ind1-1; ind++; r[ind] = ind1; ind++; r[ind] = ind2; ind++; for (int i = ind2 + 1; i < n && p_left > 0; i++) { r[ind] = i; ind++; p_left--; } } } void k2(bool *success, int n, int *a, int *res) { int l[500000]; int r[500000]; int last_min = 1000000000; for (int i = 0; i < n; i++) { if (a[i] < last_min) { last_min = a[i]; } l[i] = last_min; } int last_max = 1; for (int i = n-1; i >= 0; i--) { if (a[i] > last_max) { last_max = a[i]; } r[i] = last_max; } for (int i = 0; i < n-1; i++) { if (l[i] >= r[i+1]) { res[0] = i; res[1] = i+1; *success = true; break; } } } struct extremes { int min; int max; }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; int k; int a[500000]; int r[500000]; std::cin >> n; std::cin >> k; for (int i = 0; i < n; i++) { std::cin >> a[i]; } bool success = false; if (k > 3) { k4(&success, n, k, a, r); } else if (k == 2) { k2(&success, n, a, r); } else if (k == 3) { std::cout << "nie mam" << std::endl; } if (success) { std::cout << "TAK" << std::endl; for (int i = 0; i < k-1; i++) { std::cout << r[i] + 1; if (i < k-1) { std::cout << " "; } else { std::cout << std::endl; } } } else { std::cout << "NIE" << std::endl; } 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 | #include <iostream> void k4(bool *success, int n, int k, int *a, int *r) { int ind1 = 0; int ind2 = 0; for (int i = 0; i < n; i++) { if (a[i-1] >= a[i]) { ind1 = i-1; ind2 = i; break; } } if (ind1 != ind2) { *success = true; int p_left = k - 3; int ind = 0; for (int i = 0; i < ind1-1 && p_left > 0; i++) { r[ind] = i; ind++; p_left--; } r[ind] = ind1-1; ind++; r[ind] = ind1; ind++; r[ind] = ind2; ind++; for (int i = ind2 + 1; i < n && p_left > 0; i++) { r[ind] = i; ind++; p_left--; } } } void k2(bool *success, int n, int *a, int *res) { int l[500000]; int r[500000]; int last_min = 1000000000; for (int i = 0; i < n; i++) { if (a[i] < last_min) { last_min = a[i]; } l[i] = last_min; } int last_max = 1; for (int i = n-1; i >= 0; i--) { if (a[i] > last_max) { last_max = a[i]; } r[i] = last_max; } for (int i = 0; i < n-1; i++) { if (l[i] >= r[i+1]) { res[0] = i; res[1] = i+1; *success = true; break; } } } struct extremes { int min; int max; }; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; int k; int a[500000]; int r[500000]; std::cin >> n; std::cin >> k; for (int i = 0; i < n; i++) { std::cin >> a[i]; } bool success = false; if (k > 3) { k4(&success, n, k, a, r); } else if (k == 2) { k2(&success, n, a, r); } else if (k == 3) { std::cout << "nie mam" << std::endl; } if (success) { std::cout << "TAK" << std::endl; for (int i = 0; i < k-1; i++) { std::cout << r[i] + 1; if (i < k-1) { std::cout << " "; } else { std::cout << std::endl; } } } else { std::cout << "NIE" << std::endl; } return 0; } |