三角形牧场
源程序名 pasture.???(pas, c, cpp) 可执行文件名 pasture.exe 输入文件名 pasture.in 输出文件名 pasture.out |
【问题描述】
和所有人一样,奶牛喜欢变化。它们正在设想新造型的牧场。奶牛建筑师Hei想建造围有漂亮白色栅栏的三角形牧场。她拥有N(3≤N≤40)块木板,每块的长度Li(1≤Li≤40)都是整数,她想用所有的木板围成一个三角形使得牧场面积最大。
请帮助Hei小姐构造这样的牧场,并计算出这个最大牧场的面积。
【输入】
第1行:一个整数N
第2..N+1行:每行包含一个整数,即是木板长度。
【输出】
仅一个整数:最大牧场面积乘以100然后舍尾的结果。如果无法构建,输出-1。
【样例】
pasture.in pasture.out
5 692
1
1
3
3
4
【样例解释】
692=舍尾后的(100×三角形面积),此三角形为等边三角形,边长为4。
=============
定义状态转移
f[i,j,k]表示前i个长度,第一个背包为数值j,第二个背包为数值k这种状况是否可以得到。
最后枚举f[n,i,j]观察是否成立..得出最优解.
=============
var
n:longint;
len:array[1..40]of longint;
f:array[0..40,0..1600,0..1600]of boolean;
procedure init;
begin
assign(input,'pasture.in');
assign(output,'pasture.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
function check(a,b,c:longint):boolean;
var
t:array[1..3]of longint;
i,j:longint;
begin
check:=false;
if (a+b>c) and (a+c>b) and (b+c>a) then exit(true);
end;
procedure main;
var
i,j,k,tot:longint;
p,ans:real;
begin
fillchar(f,sizeof(f),false);
readln(n);
tot:=0;
for i:=1 to n do
begin
read(len[i]);
tot:=tot+len[i];
end;
f[0,0,0]:=true;
for i:=1 to n do
for j:=0 to tot do
for k:=0 to tot do
if f[i-1,j,k] then
begin
f[i,j+len[i],k]:=true;
f[i,j,k+len[i]]:=true;
f[i,j,k]:=true;
end;
ans:=-1;
for i:=1 to tot do
for j:=1 to tot do
if f[n,i,j] and check(i,j,tot-i-j) then
begin
p:=tot / 2;
if ans<sqrt(p*(p-i)*(p-j)*(p-(tot-i-j))) then
ans:=sqrt(p*(p-i)*(p-j)*(p-(tot-i-j)));
end;
if ans=-1 then begin writeln(trunc(ans)); terminate; end;
writeln(trunc(ans*100));
end;
begin
init;
main;
terminate;
end.