它要买的所有物品在两家商店里都有卖。第 i 件物品在第一家商店的价格是a[i],在第二家商店的价格是 b[i],两个价格都不小于 0 且不超过 10000。如果在第一家商店买的物品的总额不少于 50000,那么在第一家店买的物品都可以打95 折(价格变为原来的 0.95 倍)。
求小猪买齐所有物品所需最少的总额。
输入:第一行一个数 N。接下来N 行,每行两个数。第 i 行的两个数分别代表 a[i],b[i]。
输出:输出一行一个数,表示最少需要的总额,保留两位小数。
试补全程序。(第一空 2 分,其余 3 分)
const Inf : longint = 1000000000; threshold : longint = 50000; maxn : longint = 1000; var n, total_a, total_b, i, j, total_b_prefix : longint; a, b : array[0..1000] of longint; put_a : array[0..1000] of boolean; ans : double; f : array[0..50000] of longint; function min(a, b : double) : double; begin if a < b then exit(a); exit(b); end; function min(a, b : longint) : longint; begin if a < b then exit(a); exit(b); end; begin read(n); total_a := 0; total_b := 0; for i := 0 to n - 1 do begin read(a[i], b[i]); if a[i] <= b[i] then inc(total_a, a[i]) else inc(total_b, b[i]); end; ans := total_a + total_b; total_a := 0; total_b := 0; for i := 0 to n - 1 do begin if 1 then begin put_a[i] := true; inc(total_a, a[i]); end else begin put_a[i] := false; inc(total_b, b[i]); end; end; if 2 then begin writeln(total_a * 0.95 + total_b: 0 : 2); exit3; end; f[0] := 0; for i := 1 to threshold - 1 do f[i] := Inf; total_b_prefix := 0; for i := 0 to n - 1 do if put_a[i] = false then begin inc(total_b_prefix, b[i]); for j := threshold - 1 downto 0 do begin if (3) >= threshold) and (f[j] <> Inf) then ans := min(ans, (total_a + j + a[i]) * 0.95 + 4); if j >= a[i] then f[j] := min(f[j] + b[i], 5) else f[j] := min(f[j] + b[i], Inf); end; end; writeln(ans: 0 : 2); end.
