QOJ.ac

QOJ

时间限制: 0.5 s 内存限制: 1024 MB 总分: 100

#8514. 交易的乐趣

统计

Apolyanka 和 Büdelsdorf 是两个最近才建立联系的新石器时代小村庄。共有 $N$ 种资源,编号从 $1$ 到 $N$,每个村庄都能独立生产其中的任何一种,但效率各不相同。生产单位资源 $i$,Apolyanka 需要 $A_i$ 工时,而 Büdelsdorf 需要 $B_i$ 工时。目前,Apolyanka 在每个给定的时间段内生产 $U_i$ 单位的资源 $i$,而 Büdelsdorf 生产 $W_i$ 单位。

每个村庄目前都在以最大产能工作,也就是说,他们无法投入比现在更多的工时。然而,通过最近发现的贸易优势,两个村庄有可能在生产出所需全部资源的同时减少总工时,从而将节省下来的工时用于休息和娱乐。村庄所需要做的仅仅是合作、协调工作并交换资源。

例如,假设 $N = 2$,资源 $1$ 是木材,资源 $2$ 是食物,$A_1 = 1, U_1 = 2, B_1 = 4, W_1 = 1$,$A_2 = 2, U_2 = 1, B_2 = 3, W_2 = 4$。那么 Apolyanka 正在进行 $4$ 工时的工作:$A_1 \cdot U_1 = 2$ 用于生产 $U_1 = 2$ 单位木材,$A_2 \cdot U_2 = 2$ 用于生产 $U_2 = 1$ 单位食物。同样,Büdelsdorf 正在进行 $16$ 工时的工作:$B_1 \cdot W_1 = 4$ 用于生产 $W_1 = 1$ 单位木材,$B_2 \cdot W_2 = 12$ 用于生产 $W_2 = 4$ 单位食物。因此,总产量为 $U_1 + W_1 = 3$ 单位木材和 $U_2 + W_2 = 5$ 单位食物,共需 $4 + 16 = 20$ 工时。

然而,更好的组织方式是可能的:Apolyanka 可以生产 $3$ 单位木材和 $0.5$ 单位食物,而 Büdelsdorf 可以不生产木材,生产 $4.5$ 单位食物。每种资源的总产量保持不变,但仅需 $3A_1 + 0.5A_2 + 0B_1 + 4.5B_2 = 3 + 1 + 13.5 = 17.5$ 工时。

另一个 $N = 3$ 的例子中,$A_1 = 1, B_1 = 2, A_2 = 2, B_2 = 1, A_3 = 1, B_3 = 1$,且对于 $i = 1, 2, 3$ 有 $U_i = W_i = 1$。在这种情况下,每个村庄目前都在工作 $4$ 工时。然而,通过轻微的重组,他们可以各自工作 $3$ 工时,同时生产出完全相同的资源总量!所需要的仅仅是 Apolyanka 少生产 $1$ 单位资源 $2$ 并多生产 $1$ 单位资源 $1$,而 Büdelsdorf 则做相反的操作。

给定所有这些值,你能计算出村庄为了生产出完全相同的资源总量,所需的最少总工时是多少吗?注意,投入生产某种资源的工时数不要求是整数。

输入格式

第一行包含一个整数 $N$ ($1 \le N \le 10^5$),表示资源的数量。每种资源由 $1$ 到 $N$ 之间的一个不同整数标识。

接下来的 $N$ 行,第 $i$ 行描述资源 $i$,包含四个整数 $A_i, U_i, B_i$ 和 $W_i$ ($1 \le A_i, U_i, B_i, W_i \le 1000$,对于 $i = 1, 2, \dots, N$),含义如题目所述。

输出格式

输出一行,表示生产这些资源所需的最少总工时。输出的绝对误差或相对误差应不超过 $10^{-9}$。

样例

输入 1

2
1 2 4 1
2 1 3 4

输出 1

17.500000000000000

输入 2

3
1 1 2 1
2 1 1 1
1 1 1 1

输出 2

6.000000000000000

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.