#P3940. Harmonious Rabbit Feeding
Harmonious Rabbit Feeding
Harmonious Rabbit Feeding
Little C is preparing to feed her rabbits by dividing them into contiguous groups. There are n rabbits arranged in a row, where the i-th rabbit has color ai. Before dividing the rabbits into groups, Little C noticed an interesting property: two rabbits conflict if and only if the sum of their colors is a positive perfect square. For example, rabbits of colors 1 and 2 do not conflict (since 1+2=3 is not a perfect square), whereas rabbits of colors 1 and 3 conflict because
\(1+3=4=2^2\).
In order to keep the commotion in each group under control, Little C defines the conflict value of a group as the minimum number \(k\) such that the group can be partitioned into \(k\) (not necessarily contiguous) sub‐groups with no conflict within any subgroup. In other words, each subgroup must be an independent set in the graph where an edge between two rabbits indicates a conflict. Little C requires that the maximum conflict value among all groups does not exceed a given integer \(K\>.
Among all possible ways to partition the rabbits into contiguous groups (each group being a consecutive segment) such that each group has conflict value at most \(K\), Little C wants to choose one that uses the minimum number of groups. If there are several such schemes, she chooses the lexicographically smallest one. Here, the lexicographical order is defined on the list of break positions. A break position is an index \(i\) (\(1 \le i < n\)) such that the \(i\)-th and the \(i+1\)-th rabbits end up in different groups. The lexicographically smallest scheme is the one whose list of break positions (in increasing order) is smallest in dictionary order (i.e. at the first index where they differ, the break position in one scheme is smaller than that in the other).
Input Format:
The first line contains two integers \(n\) and \(K\) (n is the number of rabbits, \(K\) is the maximum allowed conflict value).
The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\), where \(a_i\) denotes the color of the \(i\)-th rabbit.
Output Format:
On the first line, output the minimum number of groups in the optimal partition.
On the second line, output (in increasing order) the break positions (the indices between rabbits where the partition is made). If no break is needed, output an empty line.
Note: A group is valid if the rabbits in the group can be colored with at most \(K\) colors so that no two rabbits of the same color conflict (i.e. for any two rabbits in the same subgroup, the sum of their colors is not a perfect square).
inputFormat
The first line contains two integers \(n\) and \(K\).
The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\).
outputFormat
Output the minimum number of groups on the first line, and on the second line output the break positions (indices where a break occurs between rabbits \(i\) and \(i+1\)), separated by spaces. If there are no break positions, output an empty line.
sample
4 1
1 3 4 10
2
1
</p>