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
#include <cstdio>
#include <algorithm>
#include <cassert>

#define DB if (0)
#define PDB if (0)
#define NDB if (1)
#define MAX 1000009

bool solve(int *values, int *output, int *maxi, int *mini, int *imax, int n, int k) {
    int orgN = n;

    if (imax[n-1] != n - 1) {  // last value is not the largest
        PDB printf("imax[%d-1] != %d - 1\n", n, n);
        if (k >= 3) {
            PDB printf("%d >= 3\n", k);
            output[imax[n - 1]] = 1;
            output[imax[n - 1] - 1] = 1;

            return true;
        } else {  // O(n - imax[n-1])
            // (imax[n-1] != n - 1) holds
            PDB printf("%d == 2\n", k);
            DB assert (k == 2);
            int i = imax[n - 1];

            int maxRightVal = values[n-1];

            if (maxRightVal > mini[i]) {
                // imax[n-1] must be in the left group, but now left group
                // has lower value than right, failure
                PDB printf("maxRightVal > mini[%d]\n", i);
                return false;
            }

            for (int j=n-1; j>i; j--) {
                if (mini[j-1] >= maxRightVal) {
                    output[j-1] = 1;
                    PDB printf("output[%d] = 1\n", j-1);
                    return true;
                } else {
                    maxRightVal = std::max(maxRightVal, values[j-1]);

                    if (maxRightVal > mini[i]) {
                        // imax[n-1] must be in the left group, but now left group
                        // has lower value than right, failure
                        PDB printf("maxRightVal > mini[%d]\n", i);
                        return false;
                    }
                }
            }

            return false;
        }
    } else { // imax[n-1] == n - 1, last value is the largest
        PDB printf("imax[%d-1] == %d - 1\n", n, n);
        if (k >= 3) { // TODO write test for all values equal!
            PDB printf("%d >= 3\n", k);
            while (n && imax[n-1] == n-1) n--; // still in `last is the largest` case
            PDB printf("%d == 0\n", n);
            if (n == 0) return false; // strictly increasing ordering

            DB assert (imax[n-1] != n-1);

            if (k >= 4) {
                PDB printf("%d >= 4\n", k);
                output[n - 1] = 1;
                k -= 1;
                int j = 0;
                while (k > 3 && n + j < orgN) {
                    output[n + j] = 1;
                    k--;
                    j++;
                }

                DB assert (k >= 3);
                DB assert (imax[n-1] != n - 1);
                bool res = solve(values, output, maxi, mini, imax, n, k);
                DB assert (res);
                return true;
            } else { /// what with the case when there are multiple equal max values?
                PDB printf("k == 3\n");
                // k == 3
                while (n) {
                    if (imax[n-1] == n-1) {
                        n--; // still in `last is the largest` case
                        PDB printf("push n to %d\n", n);
                    }
                    else {
                        int startMax = imax[n-1];
                        for (int i=imax[n-1]+1; i<n; i++) {
                            if (values[i] == mini[imax[n-1]]) {
                                output[n-1] = 1;
                                output[i-1] = 1;
                                return true;
                            }
                        }
                        for (int i=imax[n-1]+1; i<n; i++) {
                            if (values[i] > mini[imax[n-1]]) {
                                PDB printf("values[%d] > mini[imax[%d-1]]\n", i, n);
                                n = i - 1;
                                PDB printf("push n to %d\n", n);
                                break;
                            }
                        }

                        if (n == startMax) continue;

                       if (solve(values, output, maxi, mini, imax, n, k - 1)) {
                            output[n-1] = 1;
                            return true;
                        }
                        n = imax[n-1];
                    }
                }

                return false;
            }
        } else {
            return false;
        }
    }
}

int values[MAX], output[MAX], maxi[MAX], mini[MAX], imax[MAX];
int main() {
    int n, k;
    scanf("%d %d", &n, &k);

    for (int i=0; i<n; i++) {
        output[i] = 0;
        scanf("%d", values + i);
        if (i == 0) {
            maxi[i] = mini[i] = values[i];
            imax[i] = i;
        } else {
            maxi[i] = std::max(maxi[i-1], values[i]);
            mini[i] = std::min(mini[i-1], values[i]);

            if (maxi[i-1] < values[i]) imax[i] = i;
            else imax[i] = imax[i-1];
        }
    }

    bool doable = solve(values, output, maxi, mini, imax, n, k);

    if (doable) {
        printf("TAK\n");

        int win = 0;
        int maxPeriod, minPeriod, minPrev;
        maxPeriod = minPeriod = minPrev = values[0];
        for (int i=0; i<n-1; i++) {
            maxPeriod = std::max(maxPeriod, values[i]);
            minPeriod = std::min(minPeriod, values[i]);

            if (output[i]) {
                if (minPrev >= maxPeriod) {
                    win = 1;
                }

                minPrev = minPeriod;
                maxPeriod = minPeriod = values[i+1];
                k--;
            }
        }

        if (minPrev >= maxPeriod) {
            win = 1;
        }

        int id = 0;
        while (k > 1) {
            if (!output[id]) {
                output[id] = 1;
                k--;
            }
            id++;
        }

        DB assert (win);
        DB assert (k == 1);

        NDB for (int i=0; i<n; i++) if (output[i]) printf("%d ", i + 1);
        printf("\n");
    } else {
        printf("NIE\n");
    }
}