Pirates' Code
Time limit1sMemory limit128 MB
Given N integers from the foreheads, decide whether the set contains no increasing arithmetic progression of length 3, and if it does, print the lexicographically first witness triple.
- Level
Medium6 of 10
- Topics
- Sorting, Hash map, Brute force, Implementation
- Solved
- No attempts yet
Problem
Davy Jones has captured another ship and is smiling contentedly under the sun. Today is a good day for him: he will get more souls to serve on his crew. But the day looks so nice, the sun shining brightly and the ocean lying calm, that he decides to be merciful and give the wretched seamen of the captured ship a chance. He plays the following game.
He lines the captives up in front of him and writes a number of his choice on each man's forehead. Then he wants to check whether the set of numbers is 3-free. A set is 3-free when no three of its numbers form an increasing arithmetic progression. A triple is an increasing arithmetic progression when and — for example , , or .
If the set of numbers is 3-free, the whole group goes free (what an irony). Otherwise, the men whose numbers form the lexicographically first witness — the lexicographically first triple that proves the set is not 3-free — will serve on Jones' crew for an eternity.
You are Jones' assistant: for the given group you must decide whether it is 3-free, and if it is not, report the lexicographically first witness. Check right, or you will end up on Jones' crew too.
A triple is lexicographically smaller than when:
- , or
- and , or
- , and .
You always look at triples in increasing order, i.e. (together with this makes the three numbers distinct and strictly increasing). The numbers on the foreheads are not necessarily given in increasing order.
Input
A single line. The first integer is the number of men in the group; it is not part of the sequence. It is followed by the integers written on their foreheads.
Output
Print a single line.
If the given numbers are 3-free, print:
Sequence is 3-free.
Otherwise, print:
Sequence is not 3-free. Witness: w1,w2,w3.
where is the lexicographically first witness. The three witness numbers are separated by commas with no spaces; there is a single space after the period following 3-free. and a single space after the colon.