洛谷 P1020 导弹拦截

题目传送门

解题思路:

其实就是求一遍最长不上升子序列和最长上升子序列

AC代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm> 
 4 
 5 using namespace std;
 6 
 7 int n,a[100001],f[100001],dp[100001],len = 1,tot = 1,p;
 8 
 9 int main() {
10     while(scanf("%d",&a[++p]) != EOF);
11     f[1] = a[1];
12     dp[1] = a[1];
13     p--;
14     for(int i = 2;i <= p; i++) {
15         if(a[i] <= f[len]) 
16             f[++len] = a[i];
17         else {
18             int u = upper_bound(f+1,f+len+1,a[i],greater<int>()) - f;
19             f[u] = a[i];
20         }
21         if(a[i] > dp[tot])
22             dp[++tot] = a[i];
23         else {
24             int o = lower_bound(dp+1,dp+tot+1,a[i]) - dp;
25             dp[o] = a[i];
26         }
27     }
28     printf("%d\n%d",len,tot);
29     return 0;
30 }

 

posted @ 2019-12-10 21:49  Mr^Simon  阅读(124)  评论(0编辑  收藏  举报