
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int minBlocksToRemove(vector<int>& heights) {
int n = heights.size();
vector<int> left(n, 1), right(n, 1);
// 从左往右计算递增序列长度
for (int i = 1; i < n; ++i) {
if (heights[i] > heights[i - 1]) {
left[i] = left[i - 1] + 1;
}
}
// 从右往左计算递减序列长度
for (int i = n - 2; i >= 0; --i) {
if (heights[i] > heights[i + 1]) {
right[i] = right[i + 1] + 1;
}
}
int maxBeautifulLength = 0;
// 找出符合条件的最长美观子序列长度
for (int i = 0; i < n; ++i) {
maxBeautifulLength = max(maxBeautifulLength, left[i] + right[i] - 1);
}
// 需要移除的最小积木数
return n - maxBeautifulLength;
}
int main() {
int n;
cin >> n;
vector<int> heights(n);
for (int i = 0; i < n; ++i) {
cin >> heights[i];
}
cout << minBlocksToRemove(heights) << endl;
return 0;
}
看两个示例也没错,但一放上去就wrong answer,sos已经枯坐两小时了。