题解:P13225 [GCJ 2015 #2] Kiddie Pool
IcyWhisper · · 题解
思路
- 背包思路:
既然混合后的水温要刚好为
X ,这等价于:高温水提供的多余热量 必须等于 低温水弥补的不足热量。 因此,我们可以把水源分为三类:
- 完美水源(也就是
C_i = X ):永远全部开启,因为它自带完美温度,能白给流速。 - 冷水源(同
C_i < X ):单位流速提供(X - C_i) 的冷热量差。 - 热水源(即
C_i > X ):单位流速提供(C_i - X) 的热热量差。 设冷水源总共能提供的冷热量差最大值为H_{cold} ,热水源最大能提供的热热量差为H_{hot} 。由于二者必须平衡,那么我们在冷、热两端能实际使用的最大匹配热量即为H_{match} = \min(H_{cold}, H_{hot}) 。
- 贪心思路:
在明确了必须要凑出
H_{match} 的热量差之后,我们的目标是用最大的总流速来凑出它。
- 对于冷水源,要获得尽可能多的流速,应当优先选择温度差
(X - C_i) 最小的水源(因为代价最小,每获得1单位热量能换取的流速最大)。 - 对于热水源,同理应当优先选择温度差
(C_i - X) 最小的水源。 因此,分别将冷、热水源按温度差从小到大排序,然后贪心地往背包里装,直到凑够H_{match} 为止。
- 缩放法消精度误差:
题目保证所有的输入值最多只有 4 位小数。我们可以将所有的读入数据(
V ,X ,R_i ,C_i )直接乘以 10000 后转成整数。 由于最大的R \le 10^4 ,乘以 10000 后为10^8 ;温度C 转换后为10^6 。 最大的总热量H_{match} 不会超过100 \times 10^8 \times 10^6 = 10^{16} ,long long不会爆,而且没有任何精度损失,只有在最后计算时间时,才将整数转换回浮点数做除法。
代码
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
using namespace std;
long long parse(const string& s) {
long long res = 0;
bool decimal = false;
int dec_count = 0;
for(char c : s) {
if(c == '.') {
decimal = true;
} else if(c >= '0' && c <= '9') {
res = res * 10 +(c - '0');
if(decimal) dec_count++;
}
}
// 补齐末尾不足4位小数的部分
while(dec_count < 4) {
res = res * 10;
dec_count++;
}
return res;
}
struct Tap {
long long R_int; // 放大 10000 倍
long long delta;
};
void solve(int tc) {
int N;
string V_str, X_str;
if(!(cin >> N >> V_str >> X_str)) return;
long long V_int = parse(V_str);
long long target_T = parse(X_str);
vector<Tap> cold, hot;
double perfect_rate = 0.0;
long long max_cold_heat = 0;
long long max_hot_heat = 0;
for(int i = 0; i < N; ++i) {
string R_str, C_str;
cin >> R_str >> C_str;
long long R_int = parse(R_str);
long long C_int = parse(C_str);
if(C_int == target_T) {
perfect_rate += R_int;
} else if(C_int < target_T) {
long long delta = target_T - C_int;
cold.push_back({R_int, delta});
max_cold_heat += R_int * delta;
} else {
long long delta = C_int - target_T;
hot.push_back({R_int, delta});
max_hot_heat += R_int * delta;
}
}
long long matched_heat = min(max_cold_heat, max_hot_heat);
if(matched_heat == 0 && perfect_rate == 0.0) {
cout << "Case #" << tc << ": IMPOSSIBLE\n";
return;
}
sort(cold.begin(), cold.end(), [&](const Tap& a, const Tap& b) {
return a.delta < b.delta;
});
sort(hot.begin(), hot.end(), [&](const Tap& a, const Tap& b) {
return a.delta < b.delta;
});
double total_rate = perfect_rate;
long long current_heat = matched_heat;
for(const auto& tap : cold) {
if(current_heat == 0) break;
long long heat_cap = tap.R_int * tap.delta;
long long take_heat = min(current_heat, heat_cap);
long long q = take_heat / tap.delta;
long long rem = take_heat % tap.delta;
total_rate +=(double)q +(double)rem / tap.delta;
current_heat -= take_heat;
}
// 取出对应的热流速
current_heat = matched_heat;
for(const auto& tap : hot) {
if(current_heat == 0) break;
long long heat_cap = tap.R_int * tap.delta;
long long take_heat = min(current_heat, heat_cap);
long long q = take_heat / tap.delta;
long long rem = take_heat % tap.delta;
total_rate +=(double)q +(double)rem / tap.delta;
current_heat -= take_heat;
}
if(total_rate == 0.0
cout << "Case #" << tc << ": IMPOSSIBLE\n";
else cout << "Case #" << tc << ": " << fixed << setprecision(9) <<(double)V_int / total_rate << "\n";
}
int T;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
cin >> T;
for(int i = 1; i <= T; i++) {
solve(i);
}
return 0;
}