小明爱拦截

解题思路
导弹拦截的一半操作,求最长不上升子序列。
每个数取负,当成最长不下降子序列来做。
贪心把每个数塞入序列中,二分找位置或加入尾部、答案
+
+
++
++。
code
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int n,ans,t;
int a[100010];
int main()
{
cin>>n>>t;
a[++ans]=-t;
for(int i=2;i<=n;i++)
{
scanf("%d",&t);
if(-t>=a[ans])
a[++ans]=-t;
else
a[upper_bound(a+1,a+ans+1,-t)-a]=-t;
}
cout<<ans<<endl;
}
本文介绍了一种导弹拦截问题的算法实现,通过求解最长不上升子序列转换为最长不下降子序列问题来解决。利用贪心策略和二分查找技巧进行高效计算,并提供了完整的C++代码示例。
152

被折叠的 条评论
为什么被折叠?



