洛谷 P1020 导弹拦截 & [NOIP1999提高组](LIS,dp)

传送门


解题思路

很显然的LIS板子题,找一个最长不上升子序列和最长上升子序列即可。

关于LIS(O(n^2)和O(nlogn))

AC代码

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<cstdio>
 5 #include<cstring>
 6 using namespace std;
 7 const int maxn=100005;
 8 int dp1[maxn],dp2[maxn],a[maxn],n,cnt1,cnt2;
 9 int main()
10 {
11     memset(dp1,0x3f,sizeof(dp1));
12     do{
13         n++;
14     }while(scanf("%d",&a[n])!=EOF);
15     n--;
16     for(int i=1;i<=n;i++){
17         if(a[i]<=dp1[cnt1]) dp1[++cnt1]=a[i];
18         else dp1[upper_bound(dp1+1,dp1+cnt1+1,a[i],greater<int>())-dp1]=a[i];
19         if(a[i]>dp2[cnt2]) dp2[++cnt2]=a[i];
20         else dp2[lower_bound(dp2+1,dp2+cnt2+1,a[i])-dp2]=a[i];
21     }
22     cout<<cnt1<<endl<<cnt2;
23     return 0;
24 }

//NOIP1999提高组 t1

原文地址:https://www.cnblogs.com/yinyuqin/p/13823912.html