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
#include <algorithm>
#include <stdio.h>
#include <vector>

constexpr auto GUARD = std::numeric_limits<int>::max();

struct Square
{
    Square(int x, int y, int index) : m_x(x), m_y(y), m_index(index), m_size(GUARD)
    {
    }
    int m_x;
    int m_y;
    int m_index;
    int m_size;

    bool operator!=(const Square &s) const
    {
        return m_index != s.m_index;
    }

    void print() const
    {
        printf("SQUARE X: %3d, Y: %3d, SIZE: %10d, INDEX: %3d\n", m_x, m_y, m_size, m_index + 1);
    }
};

bool isFullRectangle(const std::vector<Square> &squares)
{
    const auto cmpMinX = [](const Square &s1, const Square &s2) { return s1.m_x <= s2.m_x; };
    const auto cmpMaxX = [](const Square &s1, const Square &s2) { return s1.m_x + s1.m_size <= s2.m_x + s2.m_size; };
    const auto cmpMinY = [](const Square &s1, const Square &s2) { return s1.m_y <= s2.m_y; };
    const auto cmpMaxY = [](const Square &s1, const Square &s2) { return s1.m_y + s1.m_size <= s2.m_y + s2.m_size; };
    const auto minX = std::min_element(std::begin(squares), std::end(squares), cmpMinX);
    const auto maxX = std::max_element(std::begin(squares), std::end(squares), cmpMaxX);
    const auto minY = std::min_element(std::begin(squares), std::end(squares), cmpMinY);
    const auto maxY = std::max_element(std::begin(squares), std::end(squares), cmpMaxY);

    long long int sum = 0;
    for (auto s : squares)
        sum += static_cast<long long int>(s.m_size) * static_cast<long long int>(s.m_size);
    const long long int width = maxX->m_x + maxX->m_size - minX->m_x;
    const long long int height = maxY->m_y + maxY->m_size - minY->m_y;
    const long long int correctedSum = width * height;
    return sum == correctedSum;
}

int countSizeWithNotSetSquares(const Square &sCount, const Square &sLimit)
{
    if (sCount.m_x <= sLimit.m_x && sCount.m_y <= sLimit.m_y)
        return std::max(sLimit.m_x - sCount.m_x, sLimit.m_y - sCount.m_y);
    return GUARD;
}

int countSizeWithSetSquares(const Square &sCount, const Square &sLimit)
{
    //if (sCount.m_size != GUARD || sLimit.m_size == GUARD)
    if (sLimit.m_size == GUARD)
        return GUARD;

    const auto topLeftX = sLimit.m_x;
    const auto topLeftY = sLimit.m_y + sLimit.m_size;
    const auto bottomRightX = sLimit.m_x + sLimit.m_size;
    const auto bottomRightY = sLimit.m_y;

    int size1 = GUARD;
    int size2 = GUARD;
    int size3 = GUARD;
    if (sCount.m_x <= topLeftX && sLimit.m_y <= sCount.m_y && sCount.m_y < topLeftY)
        size1 = topLeftX - sCount.m_x;
    if (sCount.m_y <= bottomRightY && sLimit.m_x <= sCount.m_x && sCount.m_x < bottomRightX)
        size2 = bottomRightY - sCount.m_y;
    if (sCount.m_x < sLimit.m_x && sCount.m_y < sLimit.m_y)
        size3 = std::max(sLimit.m_x - sCount.m_x, sLimit.m_y - sCount.m_y);
    //    if (sCount.m_x == 7 && sCount.m_y == 1)
    //    {
    //        printf("COUNT: ");
    //        sCount.print();
    //        printf("LIMIT: ");
    //        sLimit.print();
    //        printf("%d %d %d\n", size1, size2, size3);
    //    }
    return std::min(std::min(size1, sCount.m_size), std::min(size2, size3));
}

bool finish(std::vector<Square> squares, std::vector<Square> toSetSquares, const int xLimit)
{
    std::vector<Square> setSuqares;
    //    printf("TO SET: %d, SET: %d\n", toSetSquares.size(), squares.size());
    //    printf("LIMIT X: %d\n", xLimit);

    while (!toSetSquares.empty())
    {
        auto sCount = toSetSquares.back();
        toSetSquares.pop_back();
        int size = xLimit - sCount.m_x;
        for (const auto &s2 : setSuqares)
        {
            const auto x = s2.m_x;
            const auto y = s2.m_y + s2.m_size;
            //            printf("  ");
            //            s2.print();
            if (sCount.m_x <= x && sCount.m_y < y)
            {
                const auto newSize = x - sCount.m_x;
                //                printf("    %d\n", newSize);
                size = std::min(size, newSize);
            }
        }
        sCount.m_size = size;
        //        sCount.print();
        //        printf("\n");
        setSuqares.push_back(sCount);
    }
    std::copy(setSuqares.begin(), setSuqares.end(), std::back_inserter(squares));
    if (isFullRectangle(squares))
    {
        const auto sortSetCmd = [](const Square &s1, const Square &s2) { return s1.m_index <= s2.m_index; };
        std::sort(squares.begin(), squares.end(), sortSetCmd);
        printf("TAK");
        for (auto s : squares)
            printf(" %d", s.m_size);
        printf("\n");
        return true;
    }
    return false;
}
bool solveSingle(std::vector<Square> squares, std::vector<Square> toSetSquares)
{
    const auto s = toSetSquares.back();
    const auto cmpMaxX = [](const Square &s1, const Square &s2) { return s1.m_x + s1.m_size <= s2.m_x + s2.m_size; };
    const auto cmpMaxY = [](const Square &s1, const Square &s2) { return s1.m_y + s1.m_size <= s2.m_y + s2.m_size; };
    const auto maxX = std::max_element(std::begin(squares), std::end(squares), cmpMaxX);
    const auto maxY = std::max_element(std::begin(squares), std::end(squares), cmpMaxY);
    const auto x = maxX->m_x + maxX->m_size;
    const auto y = maxY->m_y + maxY->m_size;
    if (s.m_x < x && finish(squares, toSetSquares, x))
        return true;
    if (s.m_y < y && finish(squares, toSetSquares, s.m_x + y - maxY->m_y))
        return true;
    return false;
}

void fillSizeIfCan(std::vector<Square> &squares)
{
    for (auto &s1 : squares)
        for (const auto &s2 : squares)
            if (s1 != s2)
                s1.m_size = std::min(s1.m_size, countSizeWithSetSquares(s1, s2));
}

void solve(std::vector<Square> &squares)
{
    for (auto &s1 : squares)
        for (const auto &s2 : squares)
            if (s1 != s2)
                s1.m_size = std::min(s1.m_size, countSizeWithNotSetSquares(s1, s2));

    //    printf("SETED:\n");
    //    for (auto s : squares)
    //        s.print();
    //    printf("\n");

    fillSizeIfCan(squares);

    //    printf("SETED:\n");
    //    for (auto s : squares)
    //        s.print();
    //    printf("\n");

    const auto squareSetCmd = [](const Square &s) { return s.m_size == GUARD; };
    const auto sortSetCmd = [](const Square &s1, const Square &s2) { return s1.m_x <= s2.m_x; };
    std::vector<Square> toSetSuqares;
    std::copy_if(squares.begin(), squares.end(), std::back_inserter(toSetSuqares), squareSetCmd);
    squares.erase(std::remove_if(squares.begin(), squares.end(), squareSetCmd), squares.end());
    std::sort(toSetSuqares.begin(), toSetSuqares.end(), sortSetCmd);

    const auto cmpMaxX = [](const Square &s1, const Square &s2) { return s1.m_x <= s2.m_x; };
    const auto cmpMaxY = [](const Square &s1, const Square &s2) { return s1.m_y <= s2.m_y; };
    const auto maxX = std::max_element(std::begin(toSetSuqares), std::end(toSetSuqares), cmpMaxX);
    const auto maxY = std::max_element(std::begin(toSetSuqares), std::end(toSetSuqares), cmpMaxY);

    //    maxX->print();
    //    maxY->print();
    //    printf("SETED:\n");
    //    for (auto s : squares)
    //        s.print();
    //    printf("TO SET:\n");
    //    for (auto s : toSetSuqares)
    //        s.print();

    if (toSetSuqares.size() == 1)
    {
        if (solveSingle(squares, toSetSuqares))
            return;
    }
    else
    {
        for (const auto &s : toSetSuqares)
        {
            if (s != *maxX)
            {
                const auto limitX = maxX->m_x + s.m_y - maxX->m_y;
                if (finish(squares, toSetSuqares, limitX))
                    return;
            }
            if (s != *maxY)
            {
                const auto topY = maxY->m_y + s.m_x - maxY->m_x;
                const auto limitX = maxX->m_x + topY - maxX->m_y;
                if (finish(squares, toSetSuqares, limitX))
                    return;
            }
        }
    }
    printf("NIE\n");
}

int main(int, char **)
{
    int t, n, x, y;
    scanf("%d", &t);
    for (int i = 0; i < t; ++i)
    {
        std::vector<Square> squares;
        scanf("%d", &n);
        for (int i = 0; i < n; ++i)
        {
            scanf("%d %d", &x, &y);
            squares.emplace_back(x, y, i);
        }
        if (squares.size() == 1)
            printf("TAK 1\n");
        else
            solve(squares);
    }
    return 0;
}