#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 .
The program is executed from left to right. For the -th element :
- If , 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 -th input.
- For the given program, it is guaranteed that before executing this statement, the program has performed at least inputs. For the program you write, you must also ensure this.
In particular, an empty program of length is also valid.
Given a program of length , 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 with values being integers within , 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 , indicating the length of the program.
The second line contains non-negative integers , 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 , and , can both achieve the same functionality. It can be proven that there is no program of length that achieves the same functionality, so the answer is .
- For the input , the outputs of , , and are all , which satisfies the requirement.
- It can be verified that for any input of length with values being integers within , the two programs execute the same number of output statements, and every output value is identical.
However, the program cannot achieve the same functionality, because for the input , its output is , which is different from the original program's , so it does not have the same functionality.
The program is invalid, because when running the -rd instruction (i.e., an output statement with ), it has executed only input statement.
Sample Explanation #2.
There is no shorter program that can achieve the same functionality.
Sample Explanation #3.
A program of length , , can achieve the same functionality.
Sample Explanation #4.
Since there is no output statement, the empty program of length can achieve the same functionality.
Constraints.
For of the testdata, it is guaranteed that is non-strictly increasing, i.e., for any , we have .
For of the testdata, , , and it is guaranteed that before executing any output statement , the program has performed at least inputs.
Translated by ChatGPT 5