讨论 / 25
zhangzhan456 2011-04-20 23:57:00
点我顶贴 收藏 删除
haha
#1 cjj815@2011-04-20 23:56:00
回复 删除
var a,b:array[1..20000]of longint;

i,j,t,k,t1,t2,t3,n,s:longint;

procedure sort; {冒泡排序}

var i,j,t:longint;

begin

for i:=1 to n-1 do

for j:=1 to n-i do

if a[j]>a[j+1] then

begin

t:=a[j];

a[j]:=a[j+1];

a[j+1]:=t;

end;

end;

begin

readln(n); {读入果子种类数量}

for i:=1 to n do read(a[i]); {读书每种果子的数量}

sort; {排序}

s:=0;

t1:=1;

t2:=1;

t3:=0; {把三个位置初始化}

a[n+1]:=maxlongint;

a[n+2]:=maxlongint; {给数组a添加多两个无限大量}

for i:=1 to n do

b[i]:=maxlongint; {填充数组b}

for i:=1 to n-1 do {以下过程执行(n-1)次}

begin

if a[t1+1]<b[t2] then {当a[t1+1]小于b[t2]的时候,四个数(a[t1],a[t1+1],b[t2],b[t2+1])中a[t1]和a[t1+1]是最小的两个数}

begin

inc(t3); {合并后的结果储存位置}

b[t3]:=a[t1]+a[t1+1]; {合并两个最小的数}

inc(t1,2); {把t1的位置向后移动两位}

end

else

if a[t1]>b[t2+1] then {当a[t1]大于b[t2+1]的时候,四个数(a[t1],a[t1+1],b[t2],b[t2+1])中b[t2]和b[t2+1]是最小的两个数}

begin

inc(t3);

b[t3]:=b[t2]+b[t2+1];

inc(t2,2); {同上}

end

else

begin {最后一种情况就是a[t1]和b[t1]为四个数中最小的两个数}

inc(t3);

b[t3]:=a[t1]+b[t2];

inc(t1);

inc(t2);

end;

end;

for i:=1 to t3 do {把b数组中t3位置和t3位置以前的数加进s}

inc(s,b[i]);

writeln(s); {输出所求答案s}

end.

#2 zhangzhan456@2011-04-20 23:56:00
回复 删除
错了

#3 cjj815@2011-04-20 23:57:00
回复 删除
program p028;

var

a:array[1..1000000] of longint;

vis:array[1..1000000] of boolean;

px,py:longint;

procedure init;

var

i,n,x,y:longint;

begin

readln(n);

a[1]:=0;

for i:=1 to n-1 do

begin

readln(x,y);

a[y]:=x;

end;

readln(px,py);

end;

function find(x,y:longint):longint;

begin

vis[x]:=true;

while x<>1 do

begin

x:=a[x];

vis[x]:=true;

end;

while not vis[y] do y:=a[y];

find:=y;

end;

begin

assign(input,'p028.in'); reset(input);

assign(output,'p028.out'); rewrite(output);

init;

write(find(px,py));

close(input); close(output);

end.

查看更多回复
提交回复