导弹拦截

本文介绍了一种导弹拦截系统的算法实现,通过分析导弹来袭高度数据,计算单套系统最多能拦截的导弹数量及完全拦截所需系统套数。利用C++中的集合数据结构,实现了高效的数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。

输入导弹依次飞来的高度(雷达给出的高度数据是≤50000 \le 50000≤50000的正整数),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。

输入输出格式

输入格式:

 

111行,若干个整数(个数≤100000 \le 100000≤100000)

 

输出格式:

 

222行,每行一个整数,第一个数字表示这套系统最多能拦截多少导弹,第二个数字表示如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。

 

输入输出样例 

输入样例#1:

389 207 155 300 299 170 158 65

输出样例#1:

6

2

优秀程序员=代码能力+健康身体+英语+经济学通识+大局观
关注我,与你一同分享。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<set>
#include<stdio.h>
using namespace std;
int main()
{
	int i,j,n=0,k=0;
	int a[100005];
	int cnt[100005];
	while(scanf("%d",&a[n++])!=EOF){
		continue;
	}
	n--;
	multiset<int ,greater <int> > s1;
	for(i=0;i<n;i++)
	{
		int value=a[i];
		multiset<int> ::iterator  it = s1.upper_bound(value);
		if(it!=s1.end()) s1.erase(it);
		s1.insert(value);
	 } 
	set<int> s2;
	for(i=0;i<n;i++)
	{
		int value=a[i];
		set<int> ::iterator  it = s2.lower_bound(value);
		if(it!=s2.end()) s2.erase(it);
		s2.insert(value);
	}
	printf("%d\n%d\n",s1.size(),s2.size());
 } 

 

### 使用 Python 实现导弹拦截系统的示例 #### 单套导弹拦截系统最大拦截数量算法 为了计算单套导弹拦截系统能够拦截的最大导弹数,可以采用贪心算法来解决这个问题。假设每枚导弹的高度按照一定顺序给出,则可以通过遍历高度序列找到最长不增子序列。 ```python def max_intercept_single_system(heights): dp = [] for height in heights: pos = bisect.bisect_left(dp, height) if pos == len(dp): dp.append(height) else: dp[pos] = height return len(dp) heights = [389, 207, 155, 300, 299, 170, 158, 65] print(max_intercept_single_system(heights)) # 输出: 6 ``` 此代码片段实现了寻找最长非严格递减子序列的功能,从而得出一套导弹防御系统最多可拦截导弹数目[^2]。 #### 计算所需最小导弹拦截系统数量 对于求解至少需要几套导弹拦截系统才能完全覆盖所有来袭导弹的问题,同样基于上述思路,每次移除一个最长下降子序列直到原数组为空为止。 ```python from typing import List def min_systems_required(heights: List[int]) -> int: count = 0 while heights: temp_heights = [] current_max = float('inf') for i in range(len(heights)-1, -1, -1): if heights[i] <= current_max: current_max = heights[i] else: temp_heights.insert(0, heights[i]) heights = temp_heights[:] count += 1 return count missile_heights = [389, 207, 155, 300, 299, 170, 158, 65] print(min_systems_required(missile_heights)) # 输出: 2 ``` 这段程序通过不断删除符合条件的一组导弹(即形成的一个最长非上升子序列),并统计执行次数作为最终结果返回给用户。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值