#include <cstdio> #include <cassert> #include <cstring> #include <cstdint> #include <cstdlib> #include <ctime> #include <algorithm> using namespace std; const int max_n = 500005; int n, k; int a[max_n]; int maxa[max_n]; void calc_maxa(int x) { maxa[x] = a[x]; for(int i = x-1; i > 0; --i) { maxa[i] = max( a[i], maxa[i+1] ); } } int check2(int x) { int max_val; int min_val; min_val = a[1]; for(int i = 1; i < x; ++i) { if (a[i] < min_val) { min_val = a[i]; } max_val = maxa[i+1]; if (min_val >= max_val) { return i; } } return 0; } int check3(int x) { int max_val = 0; int max_val_pos = 0; for(int i = 1; i <= x; ++i) { if (a[i] > max_val) { max_val = a[i]; max_val_pos = i; continue; } } return (max_val_pos < x) ? max_val_pos : 0; } int checkn(int x) { for(int i = 1; i < x; ++i) { if (a[i] >= a[i+1]) { return i; } } return 0; } void print2(int pos) { printf("%d", pos); if (k == 3) { printf(" %d\n", n); } else { printf("\n"); } } void print3(int pos) { if (pos == 1) { printf("1 2\n"); } else { printf("%d %d\n", pos-1, pos); } } void printn(int pos) { int i = 1; k -= 4; while(k > 0 && i < pos-1) { printf(" %d", i); ++i; --k; } if (pos == 1) { printf(" %d %d %d", pos, pos+1, pos+2); i = pos+3; } if (pos == n-1) { printf(" %d %d %d", pos-2, pos-1, pos); i = pos+1; } if (pos > 1 && pos < n-1) { printf(" %d %d %d", pos-1, pos, pos+1); i = pos+2; } while(k > 0) { printf(" %d", i); ++i; --k; } printf("\n"); } int main() { int pos; scanf("%d %d\n", &n, &k); for(int i = 1; i <= n; ++i) { scanf("%d", a+i); } if (k > 3) { pos = checkn(n); if (pos>0) { printf("TAK\n"); printn(pos); return 0; } else { printf("NIE\n"); return 0; } } if (k == 3) { pos = check3(n); if (pos>0) { printf("TAK\n"); print3(pos); return 0; } --n; } calc_maxa(n); pos = check2(n); if (pos>0) { printf("TAK\n"); print2(pos); } else { printf("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 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 | #include <cstdio> #include <cassert> #include <cstring> #include <cstdint> #include <cstdlib> #include <ctime> #include <algorithm> using namespace std; const int max_n = 500005; int n, k; int a[max_n]; int maxa[max_n]; void calc_maxa(int x) { maxa[x] = a[x]; for(int i = x-1; i > 0; --i) { maxa[i] = max( a[i], maxa[i+1] ); } } int check2(int x) { int max_val; int min_val; min_val = a[1]; for(int i = 1; i < x; ++i) { if (a[i] < min_val) { min_val = a[i]; } max_val = maxa[i+1]; if (min_val >= max_val) { return i; } } return 0; } int check3(int x) { int max_val = 0; int max_val_pos = 0; for(int i = 1; i <= x; ++i) { if (a[i] > max_val) { max_val = a[i]; max_val_pos = i; continue; } } return (max_val_pos < x) ? max_val_pos : 0; } int checkn(int x) { for(int i = 1; i < x; ++i) { if (a[i] >= a[i+1]) { return i; } } return 0; } void print2(int pos) { printf("%d", pos); if (k == 3) { printf(" %d\n", n); } else { printf("\n"); } } void print3(int pos) { if (pos == 1) { printf("1 2\n"); } else { printf("%d %d\n", pos-1, pos); } } void printn(int pos) { int i = 1; k -= 4; while(k > 0 && i < pos-1) { printf(" %d", i); ++i; --k; } if (pos == 1) { printf(" %d %d %d", pos, pos+1, pos+2); i = pos+3; } if (pos == n-1) { printf(" %d %d %d", pos-2, pos-1, pos); i = pos+1; } if (pos > 1 && pos < n-1) { printf(" %d %d %d", pos-1, pos, pos+1); i = pos+2; } while(k > 0) { printf(" %d", i); ++i; --k; } printf("\n"); } int main() { int pos; scanf("%d %d\n", &n, &k); for(int i = 1; i <= n; ++i) { scanf("%d", a+i); } if (k > 3) { pos = checkn(n); if (pos>0) { printf("TAK\n"); printn(pos); return 0; } else { printf("NIE\n"); return 0; } } if (k == 3) { pos = check3(n); if (pos>0) { printf("TAK\n"); print3(pos); return 0; } --n; } calc_maxa(n); pos = check2(n); if (pos>0) { printf("TAK\n"); print2(pos); } else { printf("NIE\n"); } return 0; } |