Count fillings of at most 5 erased positions in a permutation so the number of pairs i<j with A_i<A_j equals S.
Hard8Dynamic programmingCombinatoricsBit manipulationNo attempts yetTime limit2sMemory limit512 MBEveryone at the algorithm camp likes sequences. Each person has a function that measures how much they like a sequence.
Seonggwan, the camp instructor, likes sorted sequences best. When Seonggwan looks at a sequence A, he immediately counts the pairs (i,j) with i<j and Ai<Aj. That count is the score Seonggwan gives the sequence.
Hongjun arrived in the morning and wrote a permutation of the numbers 1 through N on the blackboard. Seonggwan looked at the board as he walked into the lecture room and computed the score of the permutation on the spot. Over lunch he tried to recall the permutation, but only the score came back to him.
When Seonggwan returned to the room after lunch, the permutation was still on the board, but some of its numbers had been erased. He now wants to reconstruct the permutation Hongjun wrote, using the score he computed in the morning.
Given the score Seonggwan computed and the permutation currently left on the board, write a program that counts the ways to fill the erased positions so that the score is exactly that value.
The first line contains the size N of the permutation on the board and the score S computed in the morning. (1≤N≤100, 0≤S≤109)
The second line contains the permutation on the board, in order. An erased position is given as 0.
The permutation written on the board is a permutation in which every number from 1 to N appears once, and at most 5 positions are erased.
Print on the first line the number of ways to fill the permutation so that its score is S.