Bringing in forklifts
Statement
A warehouse has one conveyor belt, and N crates sit on it in a single row starting from the left. When the belt runs, the crates pass in front of the workstation one at a time, beginning with the one on the left. Each crate has a storage number written on it, and that number is the number of the shelf the crate must go on.
Forklifts do the work of picking up a crate and putting it on a shelf. When a new forklift is brought in, it picks up the crate that is passing in front of the workstation right then. After that the forklift works its way down the shelves one step at a time, so it can only pick up a crate whose storage number is exactly one less than the storage number of the crate it picked up last. It cannot pick up a crate whose number is larger than that, or smaller by two or more. Each crate is picked up by one forklift, and a forklift that has already been brought in does not leave the warehouse but waits for its turn.
Every crate that passes in front of the workstation has to be picked up by some forklift, with none left behind. Write a program that, given the storage numbers on the N crates in order from the left, finds the smallest number of forklifts needed to pick up all of the crates.
For example, suppose the storage numbers on N=5 crates, read from the left, are as in the following table.
| Position | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Storage number | 2 | 1 | 5 | 4 | 3 |
The first crate has number 2, and no forklift is working yet, so forklift 1 is brought in. Forklift 1 picked up the crate with number 2, so next it can only pick up a crate with number 1. The second crate has number 1, so forklift 1 picks it up as it comes. The third crate has number 5, and forklift 1 is now waiting for a crate with number 0, so it cannot pick that one up. Forklift 2 is therefore brought in. The fourth crate has number 4 and the fifth crate has number 3, so forklift 2 goes on to pick up both.
| Position | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Storage number | 2 | 1 | 5 | 4 | 3 |
| Forklift that picks it up | 1 | 1 | 2 | 2 | 2 |
| Newly brought in | yes | no | yes | no | no |
Two forklifts were used, so the answer in this example is 2.
Input and Output Examples
Example 1
Input
5 2 1 5 4 3
Output
2
Example 2
Input
10 12 5 15 3 12 13 16 7 20 13
Output
10
Example 3
Input
1 8
Output
1
Input
The number of crates N is given first, on the first line. N is a natural number from 1 to 1,000,000.
The second line then holds the storage numbers on the N crates, in order starting from the one placed on the left, separated by one space. Each storage number is a natural number from 1 to 1,000,000. All of the storage numbers are given on that one second line, and the same number may appear on several crates. The input is two lines in all.
Output
Print the smallest number of forklifts needed to pick up every crate on the first line.
Print one number and then a newline. Do not attach any other character or space before or after it.
Requirements
- Read the input from standard input and write the answer to standard output
- The crates pass by one at a time in the order given in the input. You cannot change that order
- A forklift can only pick up a number exactly one less than the number it picked up last
- All N storage numbers are given on the one second line
- N goes up to 1,000,000, so looking through every forklift for each crate does not finish in time
- The output is one number and a newline, nothing else
- Grading compares the output with the expected value down to the last character
- 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.
- 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 8
1
10 12 5 15 3 12 13 16 7 20 13
10
5 2 1 5 4 3
2
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.