#include <iostream> const int maxn = 500000 + 5; int a[maxn]; int minPref[maxn]; int maxSuf[maxn]; int main() { int n,k; std::cin >> n >> k; for (int i = 0 ; i < n ; i++) { std::cin >> a[i]; } minPref[0] = a[0]; for (int i = 1 ; i < n ; i++) { minPref[i] = std::min(minPref[i-1], a[i]); } maxSuf[n-1] = a[n-1]; for (int i = n-2 ; i >= 0 ; i--) { maxSuf[i] = std::max(a[i], maxSuf[i+1]); } if (k >= 4) { for (int i = 0 ; i < n - 1 ; i++) { if (a[i] >= a[i+1]) { std::cout << "TAK" << std::endl; int start = std::max(0,i - k + 2); for (int j = start ; j < start + k; j++) { std::cout << j + 1 << " "; } return 0; } } } else if (k == 2) { for (int j = 0 ; j < n - 1 ; j++) { if (minPref[j] >= maxSuf[j+1]) { std::cout << "TAK" << std::endl << j + 1; return 0; } } } else if (k == 3) { for (int i = 1 ; i < n-1 ; i++) { if (minPref[n-1] == a[i]) { std::cout << "TAK" << std::endl << i << " " << i+ 1; return 0; } } if (minPref[n-1] == a[n-1]) { std::cout << "TAK" << std::endl << n-2 << " " << n - 1; return 0; } for (int i = n-2 ; i >= 1 ; i--) { if (maxSuf[0] == a[i]) { std::cout << "TAK" << std::endl << i << " " << i+1; return 0; } } if (maxSuf[0] == a[0]) { std::cout << "TAK" << std::endl << 1 << " " << 2; return 0; } } std::cout << "NIE"; }
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 | #include <iostream> const int maxn = 500000 + 5; int a[maxn]; int minPref[maxn]; int maxSuf[maxn]; int main() { int n,k; std::cin >> n >> k; for (int i = 0 ; i < n ; i++) { std::cin >> a[i]; } minPref[0] = a[0]; for (int i = 1 ; i < n ; i++) { minPref[i] = std::min(minPref[i-1], a[i]); } maxSuf[n-1] = a[n-1]; for (int i = n-2 ; i >= 0 ; i--) { maxSuf[i] = std::max(a[i], maxSuf[i+1]); } if (k >= 4) { for (int i = 0 ; i < n - 1 ; i++) { if (a[i] >= a[i+1]) { std::cout << "TAK" << std::endl; int start = std::max(0,i - k + 2); for (int j = start ; j < start + k; j++) { std::cout << j + 1 << " "; } return 0; } } } else if (k == 2) { for (int j = 0 ; j < n - 1 ; j++) { if (minPref[j] >= maxSuf[j+1]) { std::cout << "TAK" << std::endl << j + 1; return 0; } } } else if (k == 3) { for (int i = 1 ; i < n-1 ; i++) { if (minPref[n-1] == a[i]) { std::cout << "TAK" << std::endl << i << " " << i+ 1; return 0; } } if (minPref[n-1] == a[n-1]) { std::cout << "TAK" << std::endl << n-2 << " " << n - 1; return 0; } for (int i = n-2 ; i >= 1 ; i--) { if (maxSuf[0] == a[i]) { std::cout << "TAK" << std::endl << i << " " << i+1; return 0; } } if (maxSuf[0] == a[0]) { std::cout << "TAK" << std::endl << 1 << " " << 2; return 0; } } std::cout << "NIE"; } |