QOJ.ac

QOJ

时间限制: 15 s 内存限制: 1024 MB 总分: 43

#12074. 边缘烘焙

统计

烘焙边缘

面包师 Maillard 先生擀好了一些饼干面团,并将其切成了 $N$ 块饼干,每一块都是矩形。他正准备把它们放进烤箱时,想起饼干酥脆、焦糖化的边缘味道特别好。具体来说,他认为如果所有饼干的周长之和尽可能接近 $P$ 毫米(mm)且不超过 $P$,他会最开心。(如果饼干批次边缘太多,可能会烤焦!)

对于每一块饼干,Maillard 先生可以决定保持原样,或者进行一次直线切割,将其分成两块面积相等的(不一定是矩形的)半块。(注意,这样的切割必须经过饼干的中心。)由此产生的两块新饼干不能再被切割。

如果 Maillard 先生做出最优决策,他能达到的最接近 $P$ 且不超过 $P$ 的周长之和是多少?

输入格式

输入的第一行包含测试用例的数量 $T$。接下来是 $T$ 个测试用例。每个测试用例的第一行包含两个整数 $N$ 和 $P$:饼干的数量和期望的周长之和(单位为 mm)。随后有 $N$ 行,第 $i$ 行包含两个整数 $W_i$ 和 $H_i$:第 $i$ 块饼干的宽度和高度(单位均为 mm)。

输出格式

对于每个测试用例,输出一行 Case #x: y,其中 $x$ 是测试用例编号(从 1 开始),$y$ 是一个实数:在 Maillard 先生完成切割后,所有饼干的周长之和的最大可能值,且该值不超过 $P$。如果 $y$ 与正确答案的绝对误差或相对误差不超过 $10^{-6}$,则视为正确。

数据范围

$1 \le T \le 100$。 $1 \le N \le 100$。 $1 \le W_i \le 250$,对于所有 $i$。 $1 \le H_i \le 250$,对于所有 $i$。 $P \ge 2 \times \sum (W_i + H_i)$($P$ 至少与切割前所有饼干的周长之和一样大)。 $P \le 10^8$。

样例

样例输入 1

4
1 7
1 1
2 920
50 120
50 120
1 32
7 4
3 240
10 20
20 30
30 10

样例输出 1

Case #1: 6.828427
Case #2: 920.000000
Case #3: 32.000000
Case #4: 240.000000

说明

注意最后一个样例在测试集 1 中不会出现。

在样例 1 中,只有一块饼干,它是一个边长为 1 的正方形。Maillard 先生可以从一个角切到对角,这会产生两个直角三角形,每个三角形的边长分别为 1、1 和 $\sqrt{2}$。此时周长之和为 $4 + 2 \times \sqrt{2}$;这小于 $P = 7$,但无法更接近了。

在样例 2 中,Maillard 先生可以将第一块饼干沿其长轴切割,产生两块新的 $25 \times 120$ 的矩形,并保持第二块饼干不变。总周长为 $580 + 340 = 920$,正好等于 $P$。

在样例 3 中,Maillard 先生可以将饼干切成两个梯形,每个梯形的边长分别为 2、4、5 和 5。此时新的周长之和为 32,正好等于 $P$。

在样例 4 中,初始周长之和正好等于 $P$,因此 Maillard 先生不需要进行任何切割。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.