Buggy algorithmProblem code: E5 |
All submissions for this problem are available.
Lukas was asked to be solve the following problem: 'given a permutation of numbers 1,..,n, calculate the number of inversions in it, i.e., the number of pairs of numbers i, j from 1..n, such that i<j and i comes after j in the permutation'. After thinking about the problem for some time, he has invented the following algorithm: 'for each number in the permutation, if it is located to the right of its correct position in sorted sequence, add to the output value the distance between this two positions'. Formally, Lukas's answer is given as: Sum ( max {0, i-pi[i]} ), where pi[i] i s the i-the element of the permutation .
So, for permutation 4,1,2,3, the algorithm would give the answer 0+1+1+1 = 3, and for the permutation 3,4,5,1,2, the answer 0+0+0+3+3 =6.
Obviously, this algorithm is wrong (it gives wrong results e.g. for permutation 3,2,1), but... sometimes it gives correct results.
Lucas would like to calculate what are the chances that this algorithm gives the correct result. Specifically, he wants to know how many different permutations of fixed length, which start with a given prefix (several starting numbers), lead to a correct answer to this problem.
Input
First, an integer t<100, representing the number of test cases. Each test case consists of two integers n and m, where n, 1≤n≤30, is the length of permutation, and m, 0≤m≤n, is the length of the given prefix; then, m distinct integers between 1 and n follow, describing the prefix of the permutation.
Output
For each test-case, output in how many ways we can augment the prefix to a complete permutation so that the algorithm gives correct results.
Example
Input: 3
3 0
5 2 1 4
5 1 3
Output:
5
3
9
| Author: | admin |
| Date Added: | 5-06-2009 |
| Time Limit: | 2 - 4 sec |
| Source Limit: | 50000 Bytes |
| Languages: | ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC |
Comments

Fetching successful submissions

why given algorithm is wrong for 3,2,1 sequence
According to the algorithm, the num of inversions in the sequence would be 0 0 2 while the correct answer is 3.
My solution to this problem results in a NZEC Runtime Error. I've narrowed the cause of the problem down to my parsing of the input. Is there any way to find out the exact error message?
@Josh The input for this problem is rather straight forward. You might want to do some checking at your side. If you still keep on facing this issue, mail your submission id along with the language you are using to admin@codechef.com