Official

C - Doukasen Editorial by en_translator


Among several ways to solve, we will introduce one of them.

First, we compute the time \(T\) that the two flames meet. This is expressed as follows.

\(T=\frac{1}{2}\sum_{i=1}^{N}\frac{A_i}{B_i}\)

Next, we compute the position that the left flame reaches at time \(T\).

Alternatively, we can simulate them from both ends, or use binary search.

Sample code (C++)

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n; cin >> n;
    vector<double> a(n), b(n);
    for(int i = 0; i < n; i++) cin >> a[i] >> b[i];
    double t = 0, ans = 0;
    for(int i = 0; i < n; i++) t += a[i] / b[i];
    t /= 2;
    for(int i = 0; i < n; i++){
        ans += min(a[i], t * b[i]);
        t -= min(a[i] / b[i], t);
    }
    cout << fixed << setprecision(15);
    cout << ans << endl;
}

posted:
last update: