导弹拦截(pascal)
導(dǎo)彈攔截
【問題描述】
某國為了防御敵國的導(dǎo)彈襲擊,發(fā)展出一種導(dǎo)彈攔截系統(tǒng)。但是這種導(dǎo)彈攔截系統(tǒng)有一個缺陷:雖然它的第一發(fā)炮彈能夠到達(dá)任意的高度,但是以后每一發(fā)炮彈都不能高于前一發(fā)的高度。某天,雷達(dá)捕捉到敵國的導(dǎo)彈來襲。由于該系統(tǒng)還在試用階段,所以只有一套系統(tǒng),因此有可能不能攔截所有的導(dǎo)彈。
輸入導(dǎo)彈依次飛來的高度(雷達(dá)給出的高度數(shù)據(jù)是不大于30000的正整數(shù)),計(jì)算這套系統(tǒng)最多能攔截多少導(dǎo)彈,如果要攔截所有導(dǎo)彈最少要配備多少套這種導(dǎo)彈攔截系統(tǒng)。
?
【輸入格式】:(mis.in)
8
389 207 155 300 299 170 158 65
?
【輸出格式】(mis.out)
6(最多能攔截的導(dǎo)彈數(shù))
2(要攔截所有導(dǎo)彈最少要配備的系統(tǒng)數(shù))
?
【分析】
原問題等價(jià)于求序列的最長不下降子序列的長度
設(shè)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.
轉(zhuǎn)載于:https://www.cnblogs.com/DingYi0602OIer/p/7674239.html
總結(jié)
以上是生活随笔為你收集整理的导弹拦截(pascal)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10修改mac地址
- 下一篇: 《Spring Cloud与Docker