题目描述
现在已知N件商品,和搬运它们其中每一件的费用。现在搬家公司老板Mr.B决定让我们每次任意选取2件商品。然后这2件商品只算一件商品的费用。但是这个商品的搬运费用是将选出的2个商品的费用之和除以k的运算结果。如此反复。直到只收一件商品的钱。这个就是商店要付的费用。掌柜的想尽可能的少付钱,以便将更多的钱捐给希望工程。所以请你帮他计算一下最少只用付多少钱。
var
n,k,i,j,t:longint;
a:array[1..10000]of int64;
procedure quicksort(l,h:longint);
var
i,j,mid,temp:longint;
begin
i:=l;j:=h;mid:=a[(l+h)div 2];
repeat
while a[i]<mid do inc(i);
while a[j]>mid do dec(j);
if i<=j then
begin
temp:=a[i];a[i]:=a[j];a[j]:=temp;
inc(i);dec(j);
end;
until i>j;
if i<h then quicksort(l,i);
if j>l then quicksort(j,h);
end;
begin
readln(n,k);
for i:=1 to n do
read(a[i]);
quicksort(1,n);
for i:=n-1 downto 1 do
begin
a[i]:=(a[i]+a[i+1])div k;
for j:=i-1 downto 1 do
if a[j]>a[i] then
begin
t:=a[i];a[i]:=a[j];a[j]:=t;
end;
end;
writeln(a[1]);
end.
最优解肯定是最大的加第二大的除以k。
为什么只用一次快排,然后就伪插入排序了呢?因为进行完进行商品合并以后,大部分数据是有序的,只需要伪插入就能完成整个序列的有序了。
Thanks for reading,please give a 赞。