DP-LIS and LCS

最长上升子串
f[i]=f[I-1]+1(f[I]>f[I-1])
f[I]=1;(f[I]<=f[I-1])
输出max(f(I))
最长上升子序列
f[I]=max(f[I],f[j]+1);

For example:D. Remove One Element http://codeforces.com/contest/1272/problem/D

左边扫一遍,右边扫一遍,然后好用
```c++

include

using namespace std;
int Dpl[200010], a[200010], Dpr[200010];
int n;
int main()
{
cin >> n;
for (int i = 1;i <= n;++i)
cin >> a[i];
int ans = 1;
Dpl[1] = Dpr[n] = 1;
for (int i = 2;i <= n;++i)
{
if (a[i] > a[i-1])
Dpl[i] = Dpl[i-1]+1;
else
Dpl[i] = 1;
}
for (int i = n-1;i >= 1;--i)
{
if (a[i] < a[i+1])
Dpr[i] = Dpr[i+1]+1;
else
Dpr[i] = 1;
}
for (int i = 2;i <= n;++i)
{
ans = max(ans, Dpl[i]);
if (a[i] > a[i-2])
ans = max(ans, Dpl[i-2]+Dpr[i]);
}
cout << ans;
return 0;
}

你可能感兴趣的:(DP-LIS and LCS)