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
#include <iostream>
#include <unordered_set>
#include <queue>
#include <cassert>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <numeric>
#include <assert.h>
#include <tuple>
#include <limits.h>

using namespace std;
using ull = unsigned long long;
using ll = long long;


#ifdef GGDEBUG
#define dbg printf
#else 
#define dbg //dbg
#endif

int n, k;
int in[500010];
int global_min = INT_MAX, global_max = INT_MIN;
int global_min_cnt = INT_MAX, global_max_cnt = INT_MIN;

vector<int> result;

void handle_k_eq_2() {
    dbg("k == 2\n");
    // can we split array into two?
    std::vector<int> minis, maxis;
    minis.resize(n);
    maxis.resize(n);

    minis[0] = in[0];
    for (int i = 1; i < n; ++i) {
        minis[i] = min(minis[i-1], in[i]);
    }

    maxis[n-1] = in[n-1];
    for (int i = n-2; i >= 0; --i) {
        maxis[i] = max(maxis[i+1], in[i]);
    }

    for (int i = 0; i < n-1; ++i) {
        if (minis[i] >= maxis[i+1]) {
            dbg("minis[%d] = %d\n", i, minis[i]);
            dbg("maxis[%d] = %d\n", i+1, maxis[i+1]);
            result.push_back(i + 1);
            return;
        }
    }
}

void handle_k_eq_3() {
    dbg("k == 3\n");
    dbg("global_min = %d, cnt %d\n", global_min, global_min_cnt);
    dbg("global_max = %d, cnt %d\n", global_max, global_max_cnt);
    if (global_min == in[0] && global_max == in[n-1] && global_max_cnt == 1 && global_min_cnt == 1) {
        return;
    }

    // left: global_min != in[0] || global_max != in[n-1] || global_max_cnt > 1 || global_min_cnt > 1
    if (global_min != in[0] || global_min_cnt > 1) {
        // find global_min
        int found;
        for(int i = 1; i < n; ++i) {
            if (in[i] == global_min) {
                found = i;
            }
        }
        if (found == n-1) {
            found--;
            dbg("shift1\n");
        }
        result.push_back(found);
        result.push_back(found + 1);
        return;
    }
    // left: global_max != in[n-1] || global_max_cnt > 1
    if (global_max != in[n-1] || global_max_cnt > 1) {
        // find global_max
        int found;
        for(int i = 0; i < n-1; ++i) {
            if (in[i] == global_max) {
                found = i;
            }
        }
        if (found == 0) {
            dbg("shift2\n");
            found++;
        }
        result.push_back(found);
        result.push_back(found + 1);
        return;
    }
}

void handle_k_ge_4() {
    dbg("k >= 4\n");
    // find i that in[i] >= in[i+1]
    int found;
    for(int i = 0; i < n-1; ++i) {
        if (in[i] >= in[i+1]) {
            found = i;
            break;
        }
    }

    int needed_intervals = k - 1;
    // taking (found) and (found+1) will make two intervals
    needed_intervals -= 3;

    // make intervals from 0 to found

    int i = 1;
    while (needed_intervals > 0 && i < found) {
        result.push_back(i);
        needed_intervals--;
        i++;
    }

    if (found + 2 == n) {
        found--;
    }
    if (found == 0) {
        found++;
    }

    // add needed to solve
    result.push_back(found);
    result.push_back(found+1);
    result.push_back(found+2);

    // fill till the end
    i = found + 3;
    while (needed_intervals > 0) {
        assert(i < n);
        result.push_back(i);
        needed_intervals--;
        i++;
    }
}

int main() {
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &in[i]);

        if (in[i] > global_max) {
            global_max = in[i];
            global_max_cnt = 1;
        } else if (in[i] == global_max) {
            global_max_cnt++;
        }

        if (in[i] < global_min) {
            global_min = in[i];
            global_min_cnt = 1;
        } else if (in[i] == global_min) {
            global_min_cnt++;
        }
    }

    // case 1 - input table is increasing
    {
        bool all_increasing = true;
        for (int i = 1; i < n; i++) {
            if (in[i - 1] >= in[i]) {
                all_increasing = false;
                break;
            }
        }
        if (all_increasing) {
            printf("NIE\n");
            return 0;
        }
    }

    if (k == 2) {
        handle_k_eq_2();
    } else if (k == 3) {
        handle_k_eq_3();
    } else {
        handle_k_ge_4();
    }

    if (result.size() == 0) {
        printf("NIE\n");
        return 0;
    }

    printf("TAK\n");
// #ifndef CHECKERKA
    for (auto x: result) {
        printf("%d ", x);
    }
    printf("\n");
// #endif

#ifdef CHECKERKA
    assert(result.size() == k-1);
    for (auto x: result) {
        assert(x >= 1);
        assert(x < n);
    }
    for (int i = 0; i < k-2; ++i) {
        assert(result[i] < result[i+1]);
    }

    result.push_back(n);

    vector<int> min_in_range, max_in_range;

    int processed = 0;
    for(auto split: result) {
        int mini = INT_MAX, maxi = INT_MIN;
        while (processed < split) {
            mini = min(mini, in[processed]);
            maxi = max(maxi, in[processed]);
            processed++;
        }
        min_in_range.push_back(mini);
        max_in_range.push_back(maxi);
    }

    dbg("minis: ");
    for(auto x: min_in_range) {
        dbg("%d ", x);
    }
    dbg("\n");
    dbg("maxis: ");
    for(auto x: max_in_range) {
        dbg("%d ", x);
    }
    dbg("\n");

    assert(min_in_range.size() == k);
    assert(max_in_range.size() == k);

    bool found_solution = false;
    for (int i = 0; i < k-1; ++i) {
        if (min_in_range[i] >= max_in_range[i+1]) {
            found_solution = true;
            break;
        }
    }
    assert(found_solution);
#endif

    return 0;
}