#include <algorithm>
#include <cstdio>
#include <set>
#include <vector>
constexpr int N = 500 * 1000;
int a[N];
int changed;
bool isIncr(int n) {
for (int i = 1; i < n; ++i) {
if (a[i] <= a[i - 1]) {
changed = i;
return false;
}
}
return true;
}
void check_k2(int n) {
std::vector<int> mx(n);
mx[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; --i) {
mx[i] = std::max(mx[i + 1], a[i]);
}
int mn = a[0];
for (int i = 1; i < n; ++i) {
if (mn >= mx[i]) {
printf("TAK\n");
printf("%d\n", i);
return;
}
mn = std::min(mn, a[i]);
}
printf("NIE\n");
}
void check_k3(int n) {
int mx = a[0], mn = a[0], mn_pos = 0, mx_pos = 0;
for (int i = 1; i < n; ++i) {
if (a[i] <= mn) {
mn = a[i];
mn_pos = i;
}
if (a[i] > mx) {
mx = a[i];
mx_pos = i;
}
}
if (mn_pos == 0 && mx_pos == n - 1) {
printf("NIE\n");
return;
}
printf("TAK\n");
if (mn_pos == 0) {
printf("%d %d\n", mx_pos, mx_pos + 1);
return;
}
printf("%d %d\n", mn_pos, mn_pos + 1);
}
void check_k4(int n, int k) {
std::set<int> pos;
pos.insert(changed - 1);
pos.insert(changed);
pos.insert(changed + 1);
pos.erase(0);
pos.erase(n);
for (int i = 1; i <= n; ++i) {
if (pos.size() < k - 1) {
pos.insert(i);
}
else {
break;
}
}
printf("TAK\n");
for (int p : pos) {
printf("%d ", p);
}
printf("\n");
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
if (isIncr(n)) {
printf("NIE\n");
return 0;
}
if (k == 2) {
check_k2(n);
return 0;
}
if (k == 3) {
check_k3(n);
return 0;
}
check_k4(n, 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 111 112 | #include <algorithm> #include <cstdio> #include <set> #include <vector> constexpr int N = 500 * 1000; int a[N]; int changed; bool isIncr(int n) { for (int i = 1; i < n; ++i) { if (a[i] <= a[i - 1]) { changed = i; return false; } } return true; } void check_k2(int n) { std::vector<int> mx(n); mx[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; --i) { mx[i] = std::max(mx[i + 1], a[i]); } int mn = a[0]; for (int i = 1; i < n; ++i) { if (mn >= mx[i]) { printf("TAK\n"); printf("%d\n", i); return; } mn = std::min(mn, a[i]); } printf("NIE\n"); } void check_k3(int n) { int mx = a[0], mn = a[0], mn_pos = 0, mx_pos = 0; for (int i = 1; i < n; ++i) { if (a[i] <= mn) { mn = a[i]; mn_pos = i; } if (a[i] > mx) { mx = a[i]; mx_pos = i; } } if (mn_pos == 0 && mx_pos == n - 1) { printf("NIE\n"); return; } printf("TAK\n"); if (mn_pos == 0) { printf("%d %d\n", mx_pos, mx_pos + 1); return; } printf("%d %d\n", mn_pos, mn_pos + 1); } void check_k4(int n, int k) { std::set<int> pos; pos.insert(changed - 1); pos.insert(changed); pos.insert(changed + 1); pos.erase(0); pos.erase(n); for (int i = 1; i <= n; ++i) { if (pos.size() < k - 1) { pos.insert(i); } else { break; } } printf("TAK\n"); for (int p : pos) { printf("%d ", p); } printf("\n"); } int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } if (isIncr(n)) { printf("NIE\n"); return 0; } if (k == 2) { check_k2(n); return 0; } if (k == 3) { check_k3(n); return 0; } check_k4(n, k); return 0; } |
English