codevs 1044 拦截导弹
来源:互联网 发布:如何挑选一支基金 知乎 编辑:程序博客网 时间:2024/05/10 17:25
codevs 1044 拦截导弹
题目描述 Description
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
输入描述 Input Description
输入导弹依次飞来的高度(雷达给出的高度数据是不大于30000的正整数)
输出描述 Output Description
输出这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。
样例输入 Sample Input
389 207 155 300 299 170 158 65
样例输出 Sample Output
6
2
数据范围及提示 Data Size & Hint
导弹的高度<=30000,导弹个数<=20
思路:最大拦截数就是求最长不上升子序列,拦截系统数的话可以二分找最合适的操作系统,没有满足的就再加一个系统。
题解:
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int a[30];//依次打来的导弹数 int dp[30];//求最长不上升子序列 int re[30];//记录系统当前的高度 int n;int find(int l,int r,int x){ if(l>=1&&r<=n) { int mid=(l+r)/2; if(re[mid]==x) { return mid; } else if(l==r&&re[mid]!=x) { if(re[mid-1]-x>=0) { return mid-1; } else { return mid; } } else if(re[mid]>x) { find(l,mid,x); } else { find(mid+1,r,x); } }}int main(){ int x; while(scanf("%d",&x)!=EOF) { a[++n]=x; dp[n]=1; } int ans=0; for(int i=1;i<=n;i++) { for(int j=1;j<i;j++) { if(a[j]>=a[i]) { dp[i]=max(dp[i],dp[j]+1); } } } for(int i=1;i<=n;i++) { ans=max(ans,dp[i]); } printf("%d\n",ans); int tot=1; re[1]=a[1]; for(int i=1;i<=n;i++) { int now=find(1,tot,a[i]); if(re[now]>=a[i])//当前有合适的系统来拦截 { re[now]=a[i]; } else//没有合适的系统来拦截了 { tot++; re[tot]=a[i]; } sort(re+1,re+tot+1);//二分要满足单调性 } printf("%d",tot); return 0;}
2 0
- Codevs 1044 拦截导弹
- CODEVS 1044拦截导弹
- codevs 1044 拦截导弹
- codevs 拦截导弹 1044
- codevs 拦截导弹 1044
- codevs 1044 拦截导弹
- CodeVS 1044 拦截导弹(ACM)
- codevs 1044_拦截导弹_模拟
- 【codevs】p1044 拦截导弹
- codevs 1128 导弹拦截
- codevs天梯 导弹拦截
- wikioi 1044 拦截导弹
- wiki 1044 拦截导弹
- 1044 拦截导弹
- 1044 拦截导弹
- 1044-拦截导弹
- 1044_拦截导弹
- Wiki OI 1044 拦截导弹
- Ajax
- 《JAVA泛型》
- 应用程序权限设计
- 浅谈WebService SOAP、Restful、HTTP(post/get)请求
- 友元
- codevs 1044 拦截导弹
- C++的函数模板与类模板
- TCP/IP四层模型
- Windows Installer无法启动或不见了的解决方法
- Firefox浏览器下载网页上的视频
- OAuth认证原理
- java中json字符串怎么转json对象
- Mysql入门(一)-web笔记(五)
- C++字符串分割函数