最长递增子序列的变种:https://blog.csdn.net/Doutd_y/article/details/82380287
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define Max(x,y) ((x)>(y) ? (x) :(y))
int main()
{
int n;
while (cin >> n)
{
vector<int> num(n,0);
vector<int> leftIncrease(n, 0);
vector<int> rightIncrease(n, 0);
int te;
for (int i = 0; i < n; i++)
{
cin >> te;
num[i] = te;
}
leftIncrease[0] = 1;
for (int i = 1; i < n; i++)
{
int max = 1;//每个元素的初始最长递增子序列均为1
for (int j = 0; j < i; j++)
{
if (num[i] > num[j])
max = Max(max, leftIncrease[j] + 1);
}
leftIncrease[i] = max;
}
rightIncrease[n - 1] = 1;
for (int i = n - 2; i >= 0; i--)
{
int max = 1;
for (int j = n - 1; j > i; j--)
{
if (num[i] > num[j])
max = Max(max, rightIncrease[j] + 1);
}
rightIncrease[i] = max;
}
int sumMax = 0;
for (int i = 0; i < n; i++)
{
if (leftIncrease[i] + rightIncrease[i] - 1 > sumMax)
sumMax = leftIncrease[i] + rightIncrease[i] - 1;
}
cout << n - sumMax << endl;
}
return 0;
}