caioj 1065 动态规划入门(一维一边推3:合唱队形)

时间:2021-09-08 16:04:59

就是最长上升子序列,但是要用n^2的算法。

#include<cstdio>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std; const int MAXN = 1123;
int a[MAXN], b1[MAXN], b2[MAXN], n; int main()
{
scanf("%d", &n);
REP(i, 0, n) scanf("%d", &a[i]);
REP(i, 0, n)
{
b1[i] = 1; //注意不能省!!
REP(j, 0, i)
if(a[i] > a[j])
b1[i] = max(b1[i], b1[j] + 1);
} for(int i = n - 1; i >= 0; i--)
{
b2[i] = 1; //不要忘!!
REP(j, i, n)
if(a[i] > a[j])
b2[i] = max(b2[i], b2[j] + 1);
} int ans = 0;
REP(i, 0, n)
ans = max(ans, b1[i] + b2[i] - 1);
printf("%d\n", n - ans); return 0;
}