#include <cstdio> using namespace std; bool intervals[500001]; int main() { int n, k; int vals[500001]; int prefix_min[500001]; int suffix_max[500001]; int global_max_val; int global_min_val; int global_max_idx; int global_min_idx; scanf("%d %d", &n, &k); bool srising = true; // any_fall_coords is the idx of the first element in a descending pair int any_fall_coords = -1; scanf("%d", &vals[0]); global_min_val = vals[0]; global_min_idx = 0; global_max_val = vals[0]; global_max_idx = 0; prefix_min[0] = vals[0]; for (int i = 1; i < n; ++i) { scanf("%d", &vals[i]); // min and max idx will always be NOT first/last respectively if possible if (vals[i] <= global_min_val) { global_min_val = vals[i]; global_min_idx = i; } else if (vals[i] > global_max_val) { global_max_val = vals[i]; global_max_idx = i; } // prefix mins are needed for k=2 prefix_min[i] = prefix_min[i-1]; if (vals[i] <= vals[i-1]) { srising = false; any_fall_coords = i-1; } if (vals[i] < prefix_min[i]) { prefix_min[i] = vals[i]; } } // suffix maxes are needed for k=2 suffix_max[n-1] = vals[n-1]; for (int i = n-2; i >= 0; --i) { suffix_max[i] = suffix_max[i+1]; if (vals[i] > suffix_max[i+1]) { suffix_max[i] = vals[i]; } } if (srising) { printf("NIE\n"); return 0; } if (k == 2) { for (int i = 0; i < n-1; ++i) { // split [0, i] [i+1, n-1] if (prefix_min[i] >= suffix_max[i+1]) { // +1 because the answer has 1-indexing printf("TAK\n%d\n", i+1); return 0; } } printf("NIE\n"); return 0; } else if (k == 3) { // end1 and end2 are 0-indexed ends of intervals int end1, end2; if (global_min_idx != 0) { if (global_min_idx == n-1) { // special case: split [0] [1, n-2] [i] end1 = 0; end2 = n-2; } else { // split [0, global_min-1] [global_min] [global_min+1, end] end1 = global_min_idx-1; end2 = global_min_idx; } } else if (global_max_idx != n-1) { if (global_max_idx == 0) { // special case: split [0] [1] [2, end] end1 = 0; end2 = 1; } else { // split [0, global_max-1] [global_max] [global_max+1, end] end1 = global_max_idx-1; end2 = global_max_idx; } } else { printf("NIE\n"); return 0; } printf("TAK\n%d %d\n", end1+1, end2+1); return 0; } else { // k >= 4 // find any i so that vals[i] >= vals[i+1] // put those 2 in separate 1-element intervals // i.e. we need to have: ..., afc-1] [afc] [afc+1] [ ... if (any_fall_coords == -1) { // should never happen but who knows printf("NIE\n"); return 0; } // k-3 = k-1 - 1 because we set at least 1 interval initially int intervals_left = k-2; if (any_fall_coords > 0) { intervals_left -= 1; intervals[any_fall_coords - 1] = true; } intervals[any_fall_coords] = true; if (any_fall_coords+1 != n-1) { intervals_left -= 1; intervals[any_fall_coords+1] = true; } int current_interval_idx = 0; while(intervals_left > 0) { if (!intervals[current_interval_idx]) { intervals[current_interval_idx] = true; intervals_left--; } current_interval_idx++; } printf("TAK\n"); for(int i = 0; i < n; ++i) { if (intervals[i]) { printf("%d ", i+1); } } printf("\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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <cstdio> using namespace std; bool intervals[500001]; int main() { int n, k; int vals[500001]; int prefix_min[500001]; int suffix_max[500001]; int global_max_val; int global_min_val; int global_max_idx; int global_min_idx; scanf("%d %d", &n, &k); bool srising = true; // any_fall_coords is the idx of the first element in a descending pair int any_fall_coords = -1; scanf("%d", &vals[0]); global_min_val = vals[0]; global_min_idx = 0; global_max_val = vals[0]; global_max_idx = 0; prefix_min[0] = vals[0]; for (int i = 1; i < n; ++i) { scanf("%d", &vals[i]); // min and max idx will always be NOT first/last respectively if possible if (vals[i] <= global_min_val) { global_min_val = vals[i]; global_min_idx = i; } else if (vals[i] > global_max_val) { global_max_val = vals[i]; global_max_idx = i; } // prefix mins are needed for k=2 prefix_min[i] = prefix_min[i-1]; if (vals[i] <= vals[i-1]) { srising = false; any_fall_coords = i-1; } if (vals[i] < prefix_min[i]) { prefix_min[i] = vals[i]; } } // suffix maxes are needed for k=2 suffix_max[n-1] = vals[n-1]; for (int i = n-2; i >= 0; --i) { suffix_max[i] = suffix_max[i+1]; if (vals[i] > suffix_max[i+1]) { suffix_max[i] = vals[i]; } } if (srising) { printf("NIE\n"); return 0; } if (k == 2) { for (int i = 0; i < n-1; ++i) { // split [0, i] [i+1, n-1] if (prefix_min[i] >= suffix_max[i+1]) { // +1 because the answer has 1-indexing printf("TAK\n%d\n", i+1); return 0; } } printf("NIE\n"); return 0; } else if (k == 3) { // end1 and end2 are 0-indexed ends of intervals int end1, end2; if (global_min_idx != 0) { if (global_min_idx == n-1) { // special case: split [0] [1, n-2] [i] end1 = 0; end2 = n-2; } else { // split [0, global_min-1] [global_min] [global_min+1, end] end1 = global_min_idx-1; end2 = global_min_idx; } } else if (global_max_idx != n-1) { if (global_max_idx == 0) { // special case: split [0] [1] [2, end] end1 = 0; end2 = 1; } else { // split [0, global_max-1] [global_max] [global_max+1, end] end1 = global_max_idx-1; end2 = global_max_idx; } } else { printf("NIE\n"); return 0; } printf("TAK\n%d %d\n", end1+1, end2+1); return 0; } else { // k >= 4 // find any i so that vals[i] >= vals[i+1] // put those 2 in separate 1-element intervals // i.e. we need to have: ..., afc-1] [afc] [afc+1] [ ... if (any_fall_coords == -1) { // should never happen but who knows printf("NIE\n"); return 0; } // k-3 = k-1 - 1 because we set at least 1 interval initially int intervals_left = k-2; if (any_fall_coords > 0) { intervals_left -= 1; intervals[any_fall_coords - 1] = true; } intervals[any_fall_coords] = true; if (any_fall_coords+1 != n-1) { intervals_left -= 1; intervals[any_fall_coords+1] = true; } int current_interval_idx = 0; while(intervals_left > 0) { if (!intervals[current_interval_idx]) { intervals[current_interval_idx] = true; intervals_left--; } current_interval_idx++; } printf("TAK\n"); for(int i = 0; i < n; ++i) { if (intervals[i]) { printf("%d ", i+1); } } printf("\n"); return 0; } } |