There is an empty pool that needs to be filled with water at a desired temperature. To do this, you have a row of jars placed near the border of the pool and numbered starting at 0. Each jar contains a different volume of water at a different temperature. You can pour as many jars as you want to the pool, but only if they are consecutive.
The objective is to fill at least half the capacity of the pool, without overflowing it, so that the resulting water temperature is as close as possible to a target temperature, and never more than 5 degrees hotter or warmer. We will assume that, when mixing two volumes of water, the resulting temperature of the mix is the average of the original temperatures weighted by volume.
For example, you may have 5 jars as follows:
Jar number | Volume (l) | Temperature (°C) |
---|---|---|
0 | 45 | 20 |
1 | 20 | 40 |
2 | 67 | 22 |
3 | 109 | 11 |
4 | 9 | 56 |
This way, for example, you could pour jars 1, 2 and 3 into the pool, but not only jars 1 and 3.
The resulting total volume (assuming that the pool is large enough) would be liters and the resulting temperature would be .
輸入說明 | 輸出說明 |
---|---|
The input format is as follows. An integer in a single line which says the number of problems to solve. Then, for each problem: • A line with two integers: the maximum capacity of the pool and the target temperature. • A line with one integer: the number of jars, which will not be greater than 3000. • Then, a line for each jar containing two integers: the volume of water in the jar and its temperature. |
For each problem, you have to print a line with two integers representing the first and the last jar to pour into the pool, minimizing the difference with respect to the target temperature. If there is no way to fill at least half the pool without overflowing it with water within the acceptable temperature range, the line should be ‘Not possible’. If there are many optimal solutions, you have to use the jars with the lowest numbers. |
這一題請放心大膽的用兩層巢狀迴圈去做。
我們只須不斷累加 體積 * 溫度(volume_temperature) 與 體積(volume) 並計算 溫度(temperature) = volume_temperature / volume、差距目標溫度的溫差(gap) = | temperature - target_T |,並與先前的 最小溫差(min) 比較,若比較小且溫差 < 5°C、容量(capacity) 的一半 ≤ 體積 ≤ 容量(capacity) 就更新 最小溫差、起始點、終點即可。
最後如果 最小溫差(min) 還是原本設定的 10 的話就代表不可能達成。
AC 0.02s
CPE 2星
查看更多資訊請至:https://www.tseng-school.com/