Pulling out part boxes
Statement
N part boxes sit in a row on the conveyor belt of a warehouse. Each box, numbered 1 through N, has the number of parts inside it written on it.
At the end of the belt there is a splitter that divides the parts in a box into two halves and loads them evenly onto two trucks. The splitter accepts only boxes whose part count is even. If even one box with an odd part count comes in, the splitter stops right there.
To keep the splitter from stopping, a worker may pull up to K boxes off the belt. When a box is pulled out, the boxes on either side of it come together.
Suppose the worker picks the boxes to pull out well. Write a program that computes the largest number of boxes the splitter takes in a row without stopping.
For example, suppose the part counts of N=10 boxes are given by the table below, and K=2.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Part count | 1 | 2 | 3 | 4 | 2 | 2 | 1 | 5 | 6 | 8 |
It is enough to decide first which stretch of the belt goes into the splitter as a whole, and then pull out the odd boxes mixed into that stretch. Comparing a few stretches gives the table below.
| Stretch to feed in | Boxes 2 to 7 | Boxes 4 to 10 | Boxes 2 to 10 |
|---|---|---|---|
| Odd boxes to pull out | 3, 7 | 7, 8 | 3, 7, 8 |
| Boxes to pull out | 2 | 2 | 3 |
| Boxes that pass | 4 | 5 | Cannot pull them out |
The rightmost stretch needs 3 boxes pulled out, which is more than K=2. So it cannot be used. Choosing the stretch from box 4 to box 10 and pulling out box 7 and box 8 leaves the belt as follows.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Part count | 1 | 2 | 3 | 4 | 2 | 2 | 6 | 8 |
Among the boxes left, the five at positions 4 through 8 all have an even part count. So the splitter takes five boxes in a row without stopping. No other choice of boxes to pull out lets six boxes pass in a row. So the answer for this case is 5.
Input and Output Examples
Example 1
Input
10 2 1 2 3 4 2 2 1 5 6 8
Output
5
Example 2
Input
3 1 996951714 384279646 173346001
Output
2
Example 3
Input
1 1 866420415
Output
0
Input
The first line holds the number of boxes N and the largest number of boxes that may be pulled out K, separated by a space. N is a natural number from 1 to 1,000,000. K is a natural number from 1 to 10,000, and it is never larger than N.
The second line holds the part counts of the boxes at positions 1 through N, separated by spaces, all on one line. Each part count is a natural number from 1 to 1,000,000,000.
The whole input is at most about 3.9MB. One line can hold up to 1,000,000 numbers, so read the input line by line and split each line on whitespace.
Output
Print the largest number of boxes that pass through the splitter in a row on the first line. If no box can be passed at all, print 0.
Requirements
- You do not have to pull out exactly K boxes. You may pull out fewer than K, or none at all
- When a box is pulled out, the boxes on either side of it come together
- Count only the boxes that pass in a row, and do not include the boxes you pulled out
- If no box has an even part count, the answer is 0
- N is up to 1,000,000, so checking every stretch one by one runs out of time
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 1 866420415
0
3 1 996951714 384279646 173346001
2
10 2 1 2 3 4 2 2 1 5 6 8
5
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
Nobody yet. You could be the first.