#BS0000214. Xóa ít nhất để tạo dãy hình núi (Minimum Number of Removals to Make Mountain Array)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Xóa ít nhất để tạo dãy hình núi (Minimum Number of Removals to Make Mountain Array)
Minimum Number of Removals to Make Mountain Array
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
A sequence is a mountain if and there is a peak position , with , satisfying
Given , you may remove elements anywhere without changing the relative order of the remaining elements. Find the smallest number of deletions that leaves a mountain. A feasible result is guaranteed.
Input
The first line contains the integer . The second line contains integers .
Output
Print one integer: the minimum number of deletions.
Subtasks
- Subtask 1 (20 points): ; for every ; a mountain subsequence exists.
- Subtask 2 (30 points): ; for every ; a mountain subsequence exists.
- Subtask 3 (50 points): ; for every ; a mountain subsequence exists.
Examples
Input
3
1 3 1
Output
0
Explanation
The sequence rises from to and falls to . Its peak is at position , with a nonempty side on each side, so no deletion is necessary.
Input
8
2 1 1 5 6 2 3 1
Output
3
Explanation
Delete positions to retain . With peak , at most two elements can be retained on either side. Peak allows at most one element before it and two decreasing elements after it; other peaks are smaller and cannot give a longer chain. The maximum retained length is five, so three deletions are necessary.