乱搞+动态规划——导弹拦截

来源:互联网 发布:贝叶斯定理 知乎 编辑:程序博客网 时间:2024/05/13 05:00

题目来源

洛谷P1020导弹拦截

https://www.luogu.org/problemnew/show/P1020

Vijos1303导弹拦截

https://vijos.org/p/1303


思路

最多拦截导弹数=最长不上升子序列长

最少需要系统数=最长不下降(上升)子序列长


代码(C++)

#include <cstdio>using namespace std;char c;int n=1,w=0,p=0,x,ans=0,top=0,h[100010];int f[100010]={2147483647},k[100010]={-1};int main(){scanf("%d",&n);for(int i=1;i<=n;++i){scanf("%d",&x);if(k[top]<x)k[++top]=x;if(f[ans]>=x)f[++ans]=x;for(int j=top;j>=1;--j)if(k[j-1]<x&&x<k[j])k[j]=x;for(int j=ans;j>=1;--j)if(f[j-1]>=x&&x>f[j])f[j]=x;}printf("%d\n%d",ans,top);return 0;}


原创粉丝点击