Log inSign up
Problems / B098

Taking boxes off the shelf

300 ptsCases 15Set by dongbinna
Added October 30.

Statement

A shelf in a warehouse holds boxes in a single row from left to right. There are N boxes in all, and they are called box 1, box 2 and on from the left. Each box has its weight written on it.

The warehouse manager wants to tidy the shelf. On the tidied shelf the weights of the boxes have to fall from left to right without fail. A box in front may not be lighter than a box behind it, and two boxes of the same weight may not be left on the shelf together.

The only thing the manager can do is take a box off the shelf. The boxes left behind cannot be moved to another place and their order cannot be changed. When a box is taken off, its place becomes empty, and the boxes left behind keep the order they were standing in.

Write a program that calculates the smallest number of boxes that have to be taken off when the shelf is tidied so that as many boxes as possible are left on it.

For example, suppose the weights of N=7 boxes are as in the table below.

Box1234567
Weight151148524

Taking off box 3 and box 6 leaves the boxes in the table below on the shelf.

Box left12457
Weight1511854

The weights fall from left to right, so the tidying condition is met. 5 boxes are left and 2 boxes were taken off. There is no way to meet the condition by taking off only 1 box, so the answer in this example is 2.

Box 3 and box 7 both weigh 4, so the two of them cannot be left together. When boxes of the same weight are mixed in like this, only one of them can stay.

5 boxes can also be left by keeping a different set. Taking off box 3 and box 7, for example, leaves 15, 11, 8, 5 and 2, which meets the condition as well. Whichever set is kept, 2 boxes get taken off, so the answer does not change.

There are also cases where the weights already fall from left to right at the start. No box has to be taken off then. And when there is only 1 box there is nothing to weigh it against, so no box has to be taken off either.

Input and Output Examples

Example 1

Input

7
15 11 4 8 5 2 4

Output

2

Example 2

Input

5
2407220 9506188 5579917 3823629 3963928

Output

2

Example 3

Input

1
356093

Output

0

Input

  • First the number of boxes N is given on the first line. N is a natural number from 1 to 2,000.

  • Then the weights of box 1 through box N are given in order on the second line, separated by spaces. Each weight is a natural number from 1 to 10,000,000. There may be several boxes of the same weight. The boxes are given in the order they stand, from left to right.

Output

  • Print on one line the smallest number of boxes that have to be taken off when the shelf is tidied so that as many boxes as possible are left on it. If no box has to be taken off, print 0.

Requirements

  • The boxes left behind have to fall in weight from left to right without fail
  • Two boxes of the same weight cannot be left together
  • A box cannot be moved to another place and the order cannot be changed. Taking boxes off is the only thing allowed
  • Print the number of boxes taken off, not the number of boxes left
  • 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.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
1
356093
0
5
2407220 9506188 5579917 3823629 3963928
2
7
15 11 4 8 5 2 4
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.

0 chars1 lineMemory 256MB (recorded only)Reference 31ms

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.