题解:P13225 [GCJ 2015 #2] Kiddie Pool

· · 题解

思路

  1. 背包思路: 既然混合后的水温要刚好为 X,这等价于:高温水提供的多余热量 必须等于 低温水弥补的不足热量。 因此,我们可以把水源分为三类:
  1. 贪心思路: 在明确了必须要凑出 H_{match} 的热量差之后,我们的目标是用最大的总流速来凑出它。
  1. 缩放法消精度误差: 题目保证所有的输入值最多只有 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;
}