#P4699. Team Partitioning

    ID: 17943 Type: Default 1000ms 256MiB

Team Partitioning

Team Partitioning

There are n children participating in a competition. They are to be divided into several teams. Each child i has a requirement ai, which means that the team to which child i belongs must have at least ai members (including himself/herself).

Your task is to produce a partition of the children into teams so that:

  • The number of teams is maximized.
  • Subject to the maximum number of teams, the largest team size among all teams is minimized.

It is guaranteed that a valid partition exists by possibly leaving out some children (i.e. some children might not be assigned to any team if they cannot be grouped).

Note: The optimal strategy is to greedily form teams using the children with the smallest requirements first. In other words, sort the children by their ai values, then, iteratively, accumulate children until the current group size is at least as large as the requirement of the most recent child added, form a team, and then start a new group.

This greedy algorithm yields both the maximum number of teams and minimizes the maximum team size.

inputFormat

The first line contains a single integer n (1 ≤ n ≤ 105), the number of children.

The second line contains n integers: a1, a2, ..., an (1 ≤ ai ≤ n), where ai is the minimum required team size for the i-th child.

outputFormat

First, output a single integer T representing the maximum number of teams formed.

Then output T lines, each describing a team. For each team, first output an integer k indicating the number of members in that team, followed by k space-separated integers representing the indices of the children in that team.

If there are multiple valid partitions, output any one of them.

sample

5
2 1 3 2 1
3

1 2 1 5 2 1 4

</p>