#P11061. 【MX-X4-T1】「Jason-1」IO++

【MX-X4-T1】「Jason-1」IO++

Background

Original problem link: https://oier.team/problems/X4B.

Problem Description

There is a kind of program that contains only input statements and output statements. We describe it using a non-negative integer sequence [a1,a2,][a_1, a_2, \ldots].

The program is executed from left to right. For the ii-th element aia_i:

  • If ai=0a_i = 0, it represents an input statement:
    • The program reads one integer from the input.
  • Otherwise, it represents an output statement:
    • The program outputs the integer read at the aia_i-th input.
    • For the given program, it is guaranteed that before executing this statement, the program has performed at least aia_i inputs. For the program you write, you must also ensure this.

In particular, an empty program of length 00 is also valid.

Given a program [b1,,bn][b_1, \ldots, b_n] of length nn, you need to find the minimum length of a program that can achieve the same functionality.

Two programs achieve the same functionality if and only if, for any input of length 10101010^{10^{10}} with values being integers within [1,101010][1, 10^{10^{10}}], the two programs execute the same number of output statements, and every output value is identical.

Input Format

The first line contains a positive integer nn, indicating the length of the program.

The second line contains nn non-negative integers b1,,bnb_1, \ldots, b_n, describing a program.

Output Format

Output a single integer in one line, indicating the minimum length of a program needed to achieve the same functionality.

5
0 0 0 1 2

4

7
0 0 1 0 3 1 3

7

7
0 1 0 0 2 1 0

5

4
0 0 0 0

0

Hint

Sample Explanation #1.

The programs of length 44, [0,0,1,2][0,0,1,2] and [0,1,0,2][0,1,0,2], can both achieve the same functionality. It can be proven that there is no program of length 3\leq 3 that achieves the same functionality, so the answer is 44.

  • For the input [2,3,4,][2, 3, 4, \ldots], the outputs of [0,0,0,1,2][0, 0, 0, 1, 2], [0,0,1,2][0, 0, 1, 2], and [0,1,0,2][0, 1, 0, 2] are all [2,3][2, 3], which satisfies the requirement.
  • It can be verified that for any input of length 10101010^{10^{10}} with values being integers within [1,101010][1, 10^{10^{10}}], the two programs execute the same number of output statements, and every output value is identical.

However, the program [0,1,1][0,1,1] cannot achieve the same functionality, because for the input [2,3,4,][2, 3, 4, \ldots], its output is [2,2][2, 2], which is different from the original program's [2,3][2, 3], so it does not have the same functionality.

The program [0,1,2][0,1,2] is invalid, because when running the 33-rd instruction (i.e., an output statement with ai=2a_i = 2), it has executed only 11 input statement.

Sample Explanation #2.

There is no shorter program that can achieve the same functionality.

Sample Explanation #3.

A program of length 55, [0,1,0,2,1][0,1,0,2,1], can achieve the same functionality.

Sample Explanation #4.

Since there is no output statement, the empty program of length 00 can achieve the same functionality.

Constraints.

For 50%50\% of the testdata, it is guaranteed that b1,,bnb_1, \ldots, b_n is non-strictly increasing, i.e., for any 2in2 \le i \le n, we have bi1bib_{i-1} \le b_i.

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 0bin0 \le b_i \le n, and it is guaranteed that before executing any output statement bib_i, the program has performed at least bib_i inputs.

Translated by ChatGPT 5