Problem Statement
The boarding can be described using the following simplified model. There are 2 * n cells numbered from 1 to 2 * n from left to right. There are n seats in the plane numbered from 1 to n. The seat i is located near cell n + i. There are n passengers numbered from 1 to n. Initially they stand in some order in cells 1, 2, ..., n. The order can be described using a permutation p[1], p[2], ..., p[n] of integers from 1 to n, where p[i] is the number of the passenger who initially stands in cell i. It is known that passenger i wants to take seat i during boarding.
The boarding process can be divided into primitive steps, each of which takes exactly 1 second. During each step, we can check all passengers from right to left and determine for each passenger what he/she will do according to the following rules:
- Denote the number of the passenger we're currently checking as X and the current cell of this passenger as Y.
- If Y < n + X and cell Y + 1 is currently empty, the passenger will move to this cell. It takes him exactly one step to complete this move, so at the beginning of the next step he/she will be in cell Y + 1.
- If Y < n + X, cell Y + 1 contains a passenger and the passenger at cell Y + 1 will perform a move during the current step, the passenger at cell Y will also move to the next cell during the current step (exactly as described in the previous rule).
- If Y < n + X, cell Y + 1 contains a passenger and the passenger at cell Y + 1 will not move during the current step, the passenger at cell Y will skip the current step (so he/she will still be in cell Y in the beginning of the next step).
- If Y = n + X, it means that the passenger in cell Y has reached the cell near which his seat is located. Therefore he will take a seat and it takes 74 steps to do it. So cell Y will be occupied during steps S, S+1, ..., S+73 (where S is the number of the current step) because the passenger at this cell will be taking his/her seat. In the beginning of step S+74 this cell will become empty because the passenger has completed taking the seat.
The boarding time is defined as the number of steps performed until each passenger has taken his/her seat. Obviously, the boarding time can depend on the initial order of passengers. For example, if p[1] = 1, p[2] = 2, the boarding time is 76 (during the first two steps both passengers reach the cells with their seats, and during the next 74 steps they simultaneously take their seats). And if p[1] = 2, p[1] = 1, the boarding time is 151 (after one step passenger 1 will reach the cell with his/her seat, during the next 74 steps he/she will take his/her seat and passenger 2 will wait until it's finished, and then passenger 2 will need 76 more steps to reach the required cell and take a seat).
You are given a
The initial order of passengers is considered to be good if the boarding time for this order is not greater than boardingTime. Return the number of good initial orders of passengers that match the given pattern.
Definition
- Class:
- TheBoardingDivOne
- Method:
- find
- Parameters:
- int[], int
- Returns:
- long
- Method signature:
- long find(int[] pattern, int boardingTime)
- (be sure your method is public)
Constraints
- pattern will contain between 2 and 18 elements, inclusive.
- Each element of pattern will be either -1 or between 1 and n, inclusive, where n is the number of elements in pattern.
- For each X between 1 and n, inclusive, there will be at most one occurrence of X in pattern.
- boardingTime will be between 2 and 222, inclusive.
Examples
{-1, -1}
100
Returns: 1
Here we have two possible permutations. In case of (1, 2) the boarding takes 76 seconds and in case of (2, 1) it takes 151 seconds.
{-1, 2, -1}
222
Returns: 1
The only one good order is (1, 2, 3).
{2, 1, 3, 5, 4, 6}
155
Returns: 1
Only one order matches pattern and the boarding for it takes exactly 155 seconds.
{-1, -1, -1, -1, -1, -1, -1}
198
Returns: 233
{-1, -1}
110
Returns: 1
{-1, -1, -1, -1, -1, -1}
52
Returns: 0
{-1, -1, -1, -1}
98
Returns: 1
{-1, -1, -1, -1, -1, -1, -1, -1}
157
Returns: 92
{-1, -1, -1, -1, -1, -1}
158
Returns: 88
{-1, -1}
151
Returns: 2
{-1, -1, -1, -1, -1}
156
Returns: 33
{-1, -1, -1, -1, -1, -1, -1}
155
Returns: 1
{-1, -1}
151
Returns: 2
{-1, -1, -1, -1}
155
Returns: 13
{-1, -1, -1, -1, -1, -1, -1}
158
Returns: 199
{-1, -1, -1, -1, -1, -1}
157
Returns: 82
{-1, -1, -1, -1}
154
Returns: 12
{-1, -1, -1, -1, -1}
157
Returns: 34
{-1, -1, -1, -1, -1}
153
Returns: 1
{-1, -1, -1}
151
Returns: 1
{-1, 5, 2, 4, 3, -1}
159
Returns: 0
{4, -1, -1, 3}
155
Returns: 1
{-1, -1, 4, 2}
155
Returns: 1
{-1, 4, -1, 1, -1, -1, 5}
160
Returns: 3
{-1, 2, 3, 4, 5, -1}
159
Returns: 1
{1, -1, -1, -1}
155
Returns: 5
{1, -1, -1, -1, 5, -1, -1, 8}
160
Returns: 10
{-1, 2, -1, -1, -1, -1}
155
Returns: 8
{1, -1, -1, 4, 5, -1}
159
Returns: 2
{-1, -1, 3, -1, 5, -1}
158
Returns: 2
{-1, -1, -1, -1, 8, 2, -1, -1}
157
Returns: 0
{-1, -1, 5, -1, -1, 1, 8, -1}
160
Returns: 2
{-1, -1, -1, -1, -1, -1, -1, -1}
157
Returns: 92
{-1, -1, -1, -1, 8, -1, -1, -1}
156
Returns: 0
{-1, -1, -1, -1, -1, -1, 3, -1}
160
Returns: 25
{1, 2, 3, 4, 5, 6, 7, 8}
222
Returns: 1
{1, 2, 3, 4, 5, 6, 7, 8}
82
Returns: 1
{5, 3, 6, 4, 8, 2, 1, 7}
222
Returns: 0
{1, 2, 3, 4, 5, 6, 7, 8}
81
Returns: 0
{-1, 2, 3, 4, 5, -1, -1, 8}
157
Returns: 2
{-1, -1, 3, -1, -1, 6, 7, 8}
162
Returns: 4
{-1, 2, -1, 4, -1, 6, 7, -1}
163
Returns: 1
{1, 2, -1, -1, 5, 6, -1, -1}
161
Returns: 4
{-1, -1, 3, -1, -1, 6, -1, 8}
159
Returns: 4
{-1, -1, 3, -1, 5, 6, -1, -1}
156
Returns: 1
{1, 2, -1, 4, 5, -1, -1, -1}
160
Returns: 5
{-1, -1, -1, -1, -1, -1, -1, -1}
222
Returns: 610
{-1, -1, -1}
22
Returns: 0
{-1, -1}
2
Returns: 0
{-1, -1, -1, -1, -1}
67
Returns: 0
{-1, -1, -1}
150
Returns: 1
{-1, -1}
170
Returns: 2
{-1, -1, -1, -1, -1}
195
Returns: 34
{-1, -1}
194
Returns: 2
{-1, -1, -1, -1, -1, -1, -1, -1, -1}
172
Returns: 1597
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
150
Returns: 1
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
156
Returns: 1
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
182
Returns: 196418
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
189
Returns: 1346269
{-1, -1, -1, -1, -1, -1, -1, -1, -1}
193
Returns: 1597
{-1, 2, 3, 4, 5, -1, -1, 8, -1, 10, 11, 12, -1, -1}
165
Returns: 4
{1, 2, -1, -1, -1, 6, -1, -1, 9, -1, 11, -1, 13, 14, -1}
166
Returns: 10
{1, 2, -1, -1, 5, 6, -1, -1, 9, 10, -1, 12, -1}
165
Returns: 4
{1, -1, 3, -1, -1, -1, 7, -1, -1, -1, 11, -1, 13, 14, -1, -1}
175
Returns: 44
{1, 2, -1, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15}
172
Returns: 1597
{-1, -1, -1, -1, -1, -1, 11, -1, -1, -1, -1, 12, -1, -1}
165
Returns: 0
{-1, -1, -1, -1, -1, -1, -1, 3, -1, 1, -1, -1, -1, -1, -1, -1, -1}
179
Returns: 0
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4, -1, -1, -1}
174
Returns: 5408
{-1, -1, -1, -1, -1, -1, -1, 9, -1, -1, 6, -1, -1}
162
Returns: 2
{-1, -1, -1, -1, -1, -1, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
181
Returns: 223276
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
222
Returns: 9227465
{-1, -1, -1, -1, -1, -1, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
222
Returns: 776212
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
222
Returns: 1
{1, 17, 15, 2, 14, 4, 12, 8, 6, 11, 13, 16, 18, 3, 9, 10, 5, 7}
222
Returns: 0
{-1,-1,6,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
178
Returns: 1704292
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
176
Returns: 9142502
{1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 3524578
{1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
172
Returns: 2788771
{2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 3524578
{2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
174
Returns: 3348734
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,17}
222
Returns: 3524578
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,17}
176
Returns: 3494986
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,18}
222
Returns: 3524578
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,18}
171
Returns: 2232858
{-1,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 3190383
{-1,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
172
Returns: 2626405
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,16,-1}
222
Returns: 2692538
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,16,-1}
177
Returns: 2684199
{-1,-1,5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 2268649
{-1,-1,5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
173
Returns: 2100968
{-1,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 2236950
{-1,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
170
Returns: 879452
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,18,-1}
222
Returns: 2178310
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,18,-1}
177
Returns: 2176490
{-1,-1,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 2090981
{-1,-1,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
172
Returns: 1805853
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,15,-1,-1}
222
Returns: 2056916
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,15,-1,-1}
173
Returns: 1875500
{-1,-1,-1,6,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 1802355
{-1,-1,-1,6,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
178
Returns: 1802344
{-1,-1,-1,7,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 1731056
{-1,-1,-1,7,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
172
Returns: 1444292
{-1,-1,6,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 1704444
{-1,-1,6,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
170
Returns: 609331
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,14,-1,-1,-1}
222
Returns: 1571344
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,14,-1,-1,-1}
177
Returns: 1566096
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,14,-1,-1}
222
Returns: 1571344
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,14,-1,-1}
177
Returns: 1561376
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,15,-1}
222
Returns: 1542687
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,15,-1}
179
Returns: 1542265
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,13,-1,-1,-1}
222
Returns: 1500500
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,13,-1,-1,-1}
175
Returns: 1436379
{-1,-1,-1,-1,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
222
Returns: 1461152
{-1,-1,-1,-1,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}
175
Returns: 1457578
{-1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 18, -1, -1 }
222
Returns: 36864