Lollobrigida
Time limit1sMemory limit128 MB
Given a multiset of block heights, decide whether the blocks can be arranged so the sequence alternates up and down at every position.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Math, Combinatorics
- Solved
- No attempts yet
Problem
A testing track in a hovercraft factory is built by joining standard blocks of different heights in a row. A perfectly built track is called a lollobrigida: in it, no two neighbouring blocks have equal height, and no three consecutive blocks have heights that only increase or only decrease.
More formally, let be the sequence of block heights along a track. The track is a lollobrigida if for every one of the following holds:
- and , or
- and .
For example, blocks with heights cannot form a lollobrigida: in any arrangement two blocks of height end up side by side, or one of the monotone triples or appears, and neither is allowed.
Another set of blocks, however, can form a lollobrigida, for instance ; other lollobrigidas can be built from the same set as well.
You are given several sets of blocks. For each set, decide whether its blocks can be rearranged into a lollobrigida.
Input
The first line contains the number of data sets ().
The sets follow one after another. The first line of each set contains the number of blocks (). Each of the next lines contains one integer (), the height of a block.
Output
Print exactly lines, one per data set. On the -th line print the answer for the -th set:
TAK(Polish for "yes") if a lollobrigida can be built from that set,NIE(Polish for "no") otherwise.