2-D Permutations

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 2.0s
Memory limit: 1G

Author:
Problem type

Edward spent the whole weekend brainstorming problems involving 2-D arrays. Unfortunately, they were all too hard, so he came up with this instead:

You are given N×M integers, numbered from 1 to N×M. You would like to arrange these numbers into an N×M array A. The rows are numbered from 1 to N and the columns from 1 to M. Denote the number assigned to the i-th cell from the top and the j-th cell from the left Ai,j. An arrangement is called valid if for all cells with i>1, Ai,j>Ai1,j, and for all cells with j>1, Ai,j>Ai,j1. You are also given Q queries. For each query, a number qi is given, and you must return the number of different cells qi can be placed in all valid arrangements.

Edward has no idea how to solve this problem either. Please help him solve it.

Input Specification

The first line will contain three integers N, M, and Q, the dimensions of the array and the number of queries.

The next Q lines will each contain one integer qi, as specified in the problem statement.

Output Specification

Output Q lines, the i-th line containing the number of different cells qi can be placed in all valid arrangements.

Constraints

For all subtasks:

1N,M5000

1Q106

1qiN×M

Subtask 1 [15%]

1N×M10

1Q10

Subtask 2 [35%]

1N,M,Q300

Subtask 3 [50%]

No additional constraints.

Sample Input 1

Copy
2 2 4
1
2
3
4

Sample Output 1

Copy
1
2
2
1

Explanation for Sample 1

The two valid arrangements are:

12
34
13
24

2 and 3 can be placed in two different cells, while 1 and 4 can only be placed in one.

Sample Input 2

Copy
1 3 3
1
2
3

Sample Output 2

Copy
1
1
1

Explanation for Sample 2

The only valid arrangement is:

123

Since there is only 1 valid arrangement, each number can only be placed in 1 unique cell.

Sample Input 3

Copy
5 5 1
18

Sample Output 3

Copy
11

Explanation for Sample 3

The following diagram shows the 11 possible cells 18 can be placed in. Green cells denote possible cells, while red cells denote otherwise:


Comments


  • 0
    labungloading  commented 2 days ago

    I'm confused by sample 3 as how can 18 be placed in row 1 column 5? Wouldn't the cell right under it be 10 and since 18 is not less than 10 make the arrangement false?


  • 4
    Overseas  commented on Dec. 31, 2020, 9:49 a.m. edited

    i am certain that python is not being able to handle this, got all the answer correct but always TLE for part 3.

    edit; it can, but only with pypy3 or pypy2


  • 0
    andrewtam  commented on Aug. 14, 2020, 3:57 p.m.

    Are all queries distinct?


    • 2
      pblpbl  commented on Aug. 14, 2020, 4:35 p.m.

      Not necessarily.