信息学奥赛一本通(C++版)在线评测系统 (ssoier.cn)
最长上升子序列模型
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100 + 9;
int n;
int a[N],f[N],f2[N];
void solve()
{
cin >> n;
int ans = 0;
for (int i = 1;i <= n;i ++) cin >> a[i];
for (int i = 1;i <= n;i ++)
{
f[i] = 1;
for (int j = 1;j < i;j ++)
if (a[j] < a[i])
f[i] = max(f[i],f[j] + 1);
}
for (int i = n;i >= 1;i --)
{
f2[i] = 1;
for (int j = n;j > i;j--)
if (a[j] < a[i])
f2[i] = max(f2[i],f2[j] + 1);
}
for (int i = 1;i <= n;i ++)
ans = max(ans,f[i] + f2[i] - 1);
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _ = 1;
while(_--) solve();
return 0;
}