洛谷 P1020 导弹拦截 (LIS)
第一问最长 不上升子序列,第二问最长上升子序列
套模板就好https://blog.csdn.net/qq_34416123/article/details/81358447
那个神奇的定理当作结论吧
#include<cstdio>
#include<algorithm>
#include<cstring>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
const int MAXN = 112345;
int a[MAXN], f[MAXN], n;
int main()
{
while(~scanf("%d", &a[n++])); n--;
int len = 1;
f[len] = -a[0];
REP(i, 1, n)
{
if(f[len] <= -a[i]) f[++len] = -a[i];
else f[upper_bound(f + 1, f + len + 1, -a[i]) - f] = -a[i];
}
printf("%d\n", len);
memset(f, 0, sizeof(f));
len = 1;
f[len] = a[0];
REP(i, 1, n)
{
if(f[len] < a[i]) f[++len] = a[i];
else f[lower_bound(f + 1, f + len + 1, a[i]) - f] = a[i];
}
printf("%d\n", len);
return 0;
}