#include <stdio.h> #include <algorithm> //#define TN int nums[500005]; int min_left [500005]; int max_right[500005]; void calc_minmaxlr(int n) { min_left[0] = 1000000001; for (int i = 1; i <= n; i++) min_left[i] = std::min(min_left[i-1], nums[i]); max_right[n+1] = -1000000001; for (int i = n; i >= 1; i--) max_right[i] = std::max(max_right[i+1], nums[i]); } int find_left(int n, int p) { if (nums[p] == min_left[p]) return -1; int left = 1; int right = n; while (left < right) { int q = (left + right) / 2; if (min_left[q] < nums[p]) right = q; else left = q+1; } return left; } int find_right(int n, int p) { if (nums[p] == max_right[p]) return -1; int left = 1; int right = n; while (left < right) { int q = (left + right + 1) / 2; //printf("l=%d r=%d q=%d\n", left, right, q); if (max_right[q] > nums[p]) left = q; else right = q-1; } return left; } struct event { int pos; int left; int right; int amount; }; int cmp(const void *v0, const void *v1) { event *e0 = (event *)v0; event *e1 = (event *)v1; return e0->pos - e1->pos; } event events[1000005]; #define M 4 int tree_added[1 << (M+1)]; bool tree_bel[1 << (M+1)]; void update(int x) { if (tree_added[x] != 0) { tree_bel[x] = true; } else { if (x < (1 << M)) tree_bel[x] = tree_bel[2*x] && tree_bel[2*x+1]; else tree_bel[x] = false; } } void add(int left, int right, int v) { //printf("add(%d, %d, %d)\n", l, r, v); int l = left + (1 << M) - 1; int r = right + (1 << M) + 1; while (l > 1) { if (r - l > 1) { if ((l & 1) == 0) { tree_added[l+1] += v; update(l+1); } if ((r & 1) == 1) { tree_added[r-1] += v; update(r-1); } } l /= 2; r /= 2; update(l); update(r); } } bool ask(int left, int right) { int l = left + (1 << M) - 1; int r = right + (1 << M) + 1; bool ans_l = true; bool ans_r = true; while (l > 1) { if (r - l > 1) { if ((l & 1) == 0) { if (!tree_bel[l+1]) ans_l = false; } if ((r & 1) == 1) { if (!tree_bel[r-1]) ans_r = false; } } l /= 2; r /= 2; if (tree_added[l]) ans_l = true; if (tree_added[r]) ans_r = true; } return ans_l && ans_r; } void print_tree(void) { for (int i = 1; i < (1 << (M+1)); i++) { printf("%d", tree_bel[i]); if (tree_added[i]) printf("(%d)", tree_added[i]); if (i & (i + 1)) printf(" "); else printf("\n"); } } void answer_2(int n) { calc_minmaxlr(n); for (int i = 1; i <= n-1; i++) { if (min_left[i] >= max_right[i+1]) { printf("TAK\n"); #ifndef TN printf("%d\n", i); #endif return; } } printf("NIE\n"); } void answer_3(int n) { event *cur_event = events; calc_minmaxlr(n); for (int i = 1; i <= n; i++) { int l = find_left(n, i); int r = find_right(n, i); if (l == -1 || r == -1) continue; cur_event->pos = l; cur_event->left = i+1; cur_event->right = r; cur_event->amount = +1; cur_event++; cur_event->pos = i; cur_event->left = i+1; cur_event->right = r; cur_event->amount = -1; cur_event++; //printf("%d: l=%d, r=%d\n", i, l, r); } cur_event->pos = 1000000000; /* guard */ cur_event++; qsort(events, cur_event - events, sizeof(event), cmp); cur_event = events; for (int i = 1; i <= n-2; i++) { //printf("!%d\n", i); while (cur_event->pos == i) { //printf("%d %d %d\n", cur_event->left, cur_event->right, cur_event->amount); add(cur_event->left, cur_event->right, cur_event->amount); cur_event++; //print_tree(); } if (!ask(i+2, n)) { for (int j = i+2; j <= n; j++) { if (!ask(j, j)) { printf("TAK\n"); #ifndef TN printf("%d %d\n", i, j-1); #endif return; } } } } printf("NIE\n"); } void answer_4_or_more(int n, int k) { /* left side? */ if (nums[1] >= nums[2]) { printf("TAK\n"); #ifndef TN for (int i = 1; i < k-1; i++) printf("%d ", i); printf("\n"); #endif return; } /* right side? */ if (nums[n-1] >= nums[n]) { printf("TAK\n"); #ifndef TN for (int i = n-k+1; i <= n-1 ; i++) printf("%d ", i); printf("\n"); #endif return; } /* somewhere in the middle? */ for (int i = 2; i <= n-2; i++) { if (nums[i] >= nums[i+1]) { bool isolated = (k <= i+2); int end = isolated ? k-4 : k-1; printf("TAK\n"); #ifndef TN for (int j = 1; j <= end; j++) printf("%d ", j); if (isolated) printf("%d %d %d", i-1, i, i+1); printf("\n"); #endif return; } } printf("NIE\n"); } int main(void) { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &nums[i]); if (k == 2) answer_2(n); else if (k == 3) answer_3(n); else answer_4_or_more(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 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | #include <stdio.h> #include <algorithm> //#define TN int nums[500005]; int min_left [500005]; int max_right[500005]; void calc_minmaxlr(int n) { min_left[0] = 1000000001; for (int i = 1; i <= n; i++) min_left[i] = std::min(min_left[i-1], nums[i]); max_right[n+1] = -1000000001; for (int i = n; i >= 1; i--) max_right[i] = std::max(max_right[i+1], nums[i]); } int find_left(int n, int p) { if (nums[p] == min_left[p]) return -1; int left = 1; int right = n; while (left < right) { int q = (left + right) / 2; if (min_left[q] < nums[p]) right = q; else left = q+1; } return left; } int find_right(int n, int p) { if (nums[p] == max_right[p]) return -1; int left = 1; int right = n; while (left < right) { int q = (left + right + 1) / 2; //printf("l=%d r=%d q=%d\n", left, right, q); if (max_right[q] > nums[p]) left = q; else right = q-1; } return left; } struct event { int pos; int left; int right; int amount; }; int cmp(const void *v0, const void *v1) { event *e0 = (event *)v0; event *e1 = (event *)v1; return e0->pos - e1->pos; } event events[1000005]; #define M 4 int tree_added[1 << (M+1)]; bool tree_bel[1 << (M+1)]; void update(int x) { if (tree_added[x] != 0) { tree_bel[x] = true; } else { if (x < (1 << M)) tree_bel[x] = tree_bel[2*x] && tree_bel[2*x+1]; else tree_bel[x] = false; } } void add(int left, int right, int v) { //printf("add(%d, %d, %d)\n", l, r, v); int l = left + (1 << M) - 1; int r = right + (1 << M) + 1; while (l > 1) { if (r - l > 1) { if ((l & 1) == 0) { tree_added[l+1] += v; update(l+1); } if ((r & 1) == 1) { tree_added[r-1] += v; update(r-1); } } l /= 2; r /= 2; update(l); update(r); } } bool ask(int left, int right) { int l = left + (1 << M) - 1; int r = right + (1 << M) + 1; bool ans_l = true; bool ans_r = true; while (l > 1) { if (r - l > 1) { if ((l & 1) == 0) { if (!tree_bel[l+1]) ans_l = false; } if ((r & 1) == 1) { if (!tree_bel[r-1]) ans_r = false; } } l /= 2; r /= 2; if (tree_added[l]) ans_l = true; if (tree_added[r]) ans_r = true; } return ans_l && ans_r; } void print_tree(void) { for (int i = 1; i < (1 << (M+1)); i++) { printf("%d", tree_bel[i]); if (tree_added[i]) printf("(%d)", tree_added[i]); if (i & (i + 1)) printf(" "); else printf("\n"); } } void answer_2(int n) { calc_minmaxlr(n); for (int i = 1; i <= n-1; i++) { if (min_left[i] >= max_right[i+1]) { printf("TAK\n"); #ifndef TN printf("%d\n", i); #endif return; } } printf("NIE\n"); } void answer_3(int n) { event *cur_event = events; calc_minmaxlr(n); for (int i = 1; i <= n; i++) { int l = find_left(n, i); int r = find_right(n, i); if (l == -1 || r == -1) continue; cur_event->pos = l; cur_event->left = i+1; cur_event->right = r; cur_event->amount = +1; cur_event++; cur_event->pos = i; cur_event->left = i+1; cur_event->right = r; cur_event->amount = -1; cur_event++; //printf("%d: l=%d, r=%d\n", i, l, r); } cur_event->pos = 1000000000; /* guard */ cur_event++; qsort(events, cur_event - events, sizeof(event), cmp); cur_event = events; for (int i = 1; i <= n-2; i++) { //printf("!%d\n", i); while (cur_event->pos == i) { //printf("%d %d %d\n", cur_event->left, cur_event->right, cur_event->amount); add(cur_event->left, cur_event->right, cur_event->amount); cur_event++; //print_tree(); } if (!ask(i+2, n)) { for (int j = i+2; j <= n; j++) { if (!ask(j, j)) { printf("TAK\n"); #ifndef TN printf("%d %d\n", i, j-1); #endif return; } } } } printf("NIE\n"); } void answer_4_or_more(int n, int k) { /* left side? */ if (nums[1] >= nums[2]) { printf("TAK\n"); #ifndef TN for (int i = 1; i < k-1; i++) printf("%d ", i); printf("\n"); #endif return; } /* right side? */ if (nums[n-1] >= nums[n]) { printf("TAK\n"); #ifndef TN for (int i = n-k+1; i <= n-1 ; i++) printf("%d ", i); printf("\n"); #endif return; } /* somewhere in the middle? */ for (int i = 2; i <= n-2; i++) { if (nums[i] >= nums[i+1]) { bool isolated = (k <= i+2); int end = isolated ? k-4 : k-1; printf("TAK\n"); #ifndef TN for (int j = 1; j <= end; j++) printf("%d ", j); if (isolated) printf("%d %d %d", i-1, i, i+1); printf("\n"); #endif return; } } printf("NIE\n"); } int main(void) { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &nums[i]); if (k == 2) answer_2(n); else if (k == 3) answer_3(n); else answer_4_or_more(n, k); return 0; } |