导弹拦截(pascal)
導彈攔截
【問題描述】
某國為了防御敵國的導彈襲擊,發展出一種導彈攔截系統。但是這種導彈攔截系統有一個缺陷:雖然它的第一發炮彈能夠到達任意的高度,但是以后每一發炮彈都不能高于前一發的高度。某天,雷達捕捉到敵國的導彈來襲。由于該系統還在試用階段,所以只有一套系統,因此有可能不能攔截所有的導彈。
輸入導彈依次飛來的高度(雷達給出的高度數據是不大于30000的正整數),計算這套系統最多能攔截多少導彈,如果要攔截所有導彈最少要配備多少套這種導彈攔截系統。
?
【輸入格式】:(mis.in)
8
389 207 155 300 299 170 158 65
?
【輸出格式】(mis.out)
6(最多能攔截的導彈數)
2(要攔截所有導彈最少要配備的系統數)
?
【分析】
原問題等價于求序列的最長不下降子序列的長度
設a[i]保存原序列的值,b[i]保存以a[i]為不下降子序列最后一個元素的序列長度,則:
b[i]=max(b[j])+1(1<=j<i,a[j]<=a[i]),最后b[]中的最大值就是所求。
?
【pascal代碼】
program mis(input,output);
const
maxn=100;
var
a:array[1..maxn] of integer;
h:array[1..maxn] of integer;
n:integer;
procedure init;
var i:longint;
begin
read(n);
for i:=1 to n do read(a[i]);
end;
procedure solve;
var i,j:Integer;
begin
for i:=1 to n do
begin
h[i]:=1;
for j:=1 to i-1 do
if (a[j]>=a[i]) and (h[j]+1>h[i]) then h[i]:=h[j]+1;
end;
j:=0;
for i:=1 to n do
if h[i]>j then j:=h[i];
writeln(j);
for i:=1 to n do
begin
h[i]:=1;
for j:=1 to i-1 do
if (a[j]<a[i]) and (h[j]+1>h[i]) then h[i]:=h[j]+1;
end;
j:=0;
for i:=1 to n do
if h[i]>j then j:=h[i];
writeln(j);
end;
begin
init;
solve;
end.
轉載于:https://www.cnblogs.com/DingYi0602OIer/p/7674239.html
總結
以上是生活随笔為你收集整理的导弹拦截(pascal)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10修改mac地址
- 下一篇: 《Spring Cloud与Docker