#QHD0000088. Giá mua thấp nhất (Lowest Price in Town)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Giá mua thấp nhất (Lowest Price in Town)
Lowest Price in Town
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
A store sells a single product individually and offers several bulk packages. Each package specifies a number of items and a fixed price. The customer may buy unlimited copies of any package and may combine packages with individual purchases.
For each requested quantity , find the minimum total cost of buying at least items. It is allowed to buy more than required if this is cheaper; unused extra items have no resale value. There are no discounts other than the explicitly listed packages.
For instance, if the individual price is $22.00, a two-item package costs $22.00 and a four-item package costs $40.00, then buying four items is the cheapest way to obtain at least three: it costs $40.00 instead of $44.00 for a two-item package plus one individual item.
All monetary values have exactly two decimal places. Implementations should work in integer cents to avoid floating-point rounding errors. All queries refer to the same price list.
Input
- The first line gives the single-item price and the number of package offers.
- Each of the next lines contains a package quantity and its price .
- The last line contains positive integer queries , separated by spaces. There is no leading query count: all integers on the final line are queries.
This describes one dataset from the original UVa problem; multiple queries remain inside that dataset.
Output
First print exactly Case 1:. For each requested quantity in input order, print Buy k for $d.cc, replacing k with the requested quantity and d.cc with the minimum price in dollars, always with exactly two digits after the decimal point. Do not add blank lines.
Subtasks
-
Subtask 1 (20 points): , , , ; all prices satisfy and have exactly two decimal digits.
-
Subtask 2 (30 points): , , , ; prices .
-
Subtask 3 (50 points): , , , ; all prices satisfy .
Examples
Example 1
Input
22.00 2
2 22.00
4 60.00
2 4
Output
Case 1:
Buy 2 for $22.00
Buy 4 for $44.00
Explanation: For , one two-item package costs k=4$, two two-item packages cost $44.00, less than the four-item offer at $60.00.
Example 2
Input
22.00 2
2 22.00
4 40.00
1 2 3
Output
Case 1:
Buy 1 for $22.00
Buy 2 for $22.00
Buy 3 for $40.00
Explanation: One item is bought individually, two via the two-item package, and three via the four-item package with one extra item, at $40.00.