Range Editing
Time limit4sMemory limit2048 MB
Given a row of n cells with target values (0 means empty), find the minimum number of range assign and range clear operations to reach them.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Intervals
- Solved
- No attempts yet
Problem
A spreadsheet has a row of cells. Initially all cells are empty. You can perform two types of operations:
- Select a consecutive range of cells and change their values to a positive integer of your choice. All cells in the range get the same value after this operation.
- Select a consecutive range of cells and delete their values. All cells in the range become empty after this operation.
Given the final cell values you want, calculate the minimum number of editing operations required to obtain them.
Input
The first line contains a single integer (), the number of cells. The cells are numbered from to .
Each of the next lines contains a single integer between and inclusive. The integer on the -th line is if cell should be empty after all operations. Otherwise, it is the final value of cell .
Output
Print a single integer, the minimum number of editing operations required.