#include <iostream> inline int min(const int a, const int b) {return (a < b ? a : b);} inline int max(const int a, const int b) {return (a > b ? a : b);} constexpr int MAX_LENGTH = 500000; int tab[MAX_LENGTH+1], dp[MAX_LENGTH+1]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int length, segments; std::cin >> length >> segments; for(int i = 0; i < length; ++i) std::cin >> tab[i]; int border = -1; switch(segments) { case 2: { dp[length-1] = tab[length-1]; for(int i = length-2; i >= 0; --i) dp[i] = max(tab[i], dp[i+1]); for(int i = 0, cur_min = tab[0]; i < length-1; cur_min = min(cur_min, tab[++i])) if(cur_min >= dp[i+1]) { border = i; break; } if(border != -1) std::cout << "TAK\n" << border+1 << '\n'; else std::cout << "NIE\n"; break; } case 3: { for(int i = 1, cur_min = tab[0]; i < length-1; cur_min = min(cur_min, tab[i++])) if(cur_min >= tab[i]) { border = i; break; } if(border != -1) std::cout << "TAK\n" << border << ' ' << border+1 << '\n'; else { for(int i = length-2, cur_max = tab[length-1]; i >= 0; cur_max = max(cur_max, tab[i--])) if(tab[i] >= cur_max) { border = i; break; } if(border != -1) std::cout << "TAK\n" << border << ' ' << border+1 << '\n'; else std::cout << "NIE\n"; } break; } default: { for(int i = 0; i < length-1; ++i) if(tab[i] >= tab[i+1]) { border = i; break; } if(border != -1) { std::cout << "TAK\n"; dp[border] = dp[border+1] = dp[border+2] = 1; for(int i = 1; i < length; ++i) if(dp[i]) --segments; for(int i = 1; i < length && segments > 1; ++i) if(!dp[i]) dp[i] = 1, --segments; for(int i = 1; i < length; ++i) if(dp[i]) std::cout << i << ' '; std::cout << '\n'; } else std::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 | #include <iostream> inline int min(const int a, const int b) {return (a < b ? a : b);} inline int max(const int a, const int b) {return (a > b ? a : b);} constexpr int MAX_LENGTH = 500000; int tab[MAX_LENGTH+1], dp[MAX_LENGTH+1]; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int length, segments; std::cin >> length >> segments; for(int i = 0; i < length; ++i) std::cin >> tab[i]; int border = -1; switch(segments) { case 2: { dp[length-1] = tab[length-1]; for(int i = length-2; i >= 0; --i) dp[i] = max(tab[i], dp[i+1]); for(int i = 0, cur_min = tab[0]; i < length-1; cur_min = min(cur_min, tab[++i])) if(cur_min >= dp[i+1]) { border = i; break; } if(border != -1) std::cout << "TAK\n" << border+1 << '\n'; else std::cout << "NIE\n"; break; } case 3: { for(int i = 1, cur_min = tab[0]; i < length-1; cur_min = min(cur_min, tab[i++])) if(cur_min >= tab[i]) { border = i; break; } if(border != -1) std::cout << "TAK\n" << border << ' ' << border+1 << '\n'; else { for(int i = length-2, cur_max = tab[length-1]; i >= 0; cur_max = max(cur_max, tab[i--])) if(tab[i] >= cur_max) { border = i; break; } if(border != -1) std::cout << "TAK\n" << border << ' ' << border+1 << '\n'; else std::cout << "NIE\n"; } break; } default: { for(int i = 0; i < length-1; ++i) if(tab[i] >= tab[i+1]) { border = i; break; } if(border != -1) { std::cout << "TAK\n"; dp[border] = dp[border+1] = dp[border+2] = 1; for(int i = 1; i < length; ++i) if(dp[i]) --segments; for(int i = 1; i < length && segments > 1; ++i) if(!dp[i]) dp[i] = 1, --segments; for(int i = 1; i < length; ++i) if(dp[i]) std::cout << i << ' '; std::cout << '\n'; } else std::cout << "NIE\n"; } } return 0; } |