Under construction

Still being built. Things may change

Landing Raid

Time limit6sMemory limit1024 MB

Summary
Choose k of n soldiers so the chosen subsequence of permutation a has the fewest inversions, then count the optimal choices for each k.
Level

Hard9 of 10

Topics
Divide and conquer, Combinatorics, Dynamic programming
Solved
No attempts yet

Statement

For many years Bitocja has regularly raided Bajtocja, stealing its natural and intellectual resources. This time the Bajtocjans will strike first against the treacherous Bitocjan people. The first step of a carefully planned strategy is a landing on the beach of Bitobajtan.

The operation must be discreet, so a squad of exactly kk members of the elite unit Bajtogrom will be sent to the beach. The unit currently has nn soldiers, numbered from 1 to nn. Soldier ii has hand-to-hand combat level ii and ranged combat level aia_i. The sequence a1,…,ana_1, \ldots, a_n is a permutation of the numbers from 1 to nn. A higher level means the soldier is more advanced in that discipline.

In a well-run special unit, everyone must know whom to obey and whom they may give orders to. If two soldiers ii and jj are both sent on the mission, with i<ji < j and ai>aja_i > a_j, friction may occur between them. They will quarrel over who outranks whom in Bajtogrom.

When choosing kk soldiers for the landing, the squad must be chosen so that the number of pairs that may cause friction is as small as possible. Find that minimum number of pairs. Also find the number of ways to choose a squad of kk soldiers that reaches this minimum. Do this for every kk from 1 to nn.

Input

The first line contains one integer nn (1≤n≤401 \le n \le 40), the number of soldiers in Bajtogrom.

The second line contains nn integers a1,…,ana_1, \ldots, a_n (1≤ai≤n1 \le a_i \le n, ai≠aja_i \ne a_j for i≠ji \ne j), where aia_i is the ranged combat level of soldier ii.

Output

Print nn lines, each with two integers. On line kk, print the minimum number of pairs that may cause friction when exactly kk soldiers are sent, followed by the number of ways to reach that minimum.

Hint

For k=1k=1, no pair of soldiers can cause friction, and the single soldier can be chosen in 5 ways.

For k=2k=2, one of the pairs (2, 4), (3, 4), or (3, 5) must be chosen, so that no friction occurs.

For k=3k=3, sending (2, 3, 4) or (3, 4, 5) leaves only one pair that may cause friction.

For k=4k=4, it is best to send everyone except the first soldier. The first soldier is the weakest in hand-to-hand combat because of his index, but has the highest ranged level, a1=5a_1 = 5, so he may cause friction with every other soldier.

For k=5k=5, seven pairs may cause friction.

Examples1

  1. Example 1

    Input
    5
    5 3 1 4 2
    
    Expected output
    0 5
    0 3
    1 2
    3 1
    7 1