#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;

int main() {
    // Hypothetical measurements; temperatures in Celsius.
    const double Ta = 20.0, T0 = 37.0;
    const double T2 = 30.0, T3 = 28.0;
    const double tolerance = 1e-6;
    const double k = log((T2 - Ta) / (T3 - Ta));

    // t = hours elapsed from death to the 2:00 PM reading.
    auto f = [&](double t) {
        return Ta + (T0 - Ta) * exp(-k * t) - T2;
    };

    double a = 0.0, b = 8.0;
    double fa = f(a), fb = f(b);
    if (fa * fb >= 0) {
        cerr << "Invalid interval: no sign change.\n";
        return 1;
    }

    double mid = 0.0;
    int steps = 0;
    for (int i = 1; i <= 100; ++i) {
        mid = (a + b) / 2.0;
        double fm = f(mid);
        steps = i;
        if (abs(fm) < 1e-8 || (b - a) / 2 < tolerance)
            break;
        if (fa * fm > 0) {
            a = mid;
            fa = fm;
        } else {
            b = mid;
        }
    }

    // Observation was at 14:00; subtract elapsed seconds.
    int seconds = 14 * 3600 - (int)round(mid * 3600);
    int h24 = (seconds / 3600) % 24;
    int minute = (seconds % 3600) / 60;
    int second = seconds % 60;
    int h12 = (h24 % 12 == 0) ? 12 : h24 % 12;

    cout << fixed << setprecision(6);
    cout << "Cooling constant: " << k << " per hour\n";
    cout << "Bisection iterations: " << steps << "\n";
    cout << setprecision(4);
    cout << "Estimated time since death: " << mid << " hours\n";
    cout << "Estimated time of death: " << setfill('0')
         << setw(2) << h12 << ':' << setw(2) << minute
         << ':' << setw(2) << second
         << (h24 < 12 ? " AM" : " PM") << "\n";
    cout << "Model temperature at 2 PM: "
         << Ta + (T0 - Ta) * exp(-k * mid) << " C\n";
    return 0;
}
