Official

B - Humidifier 1 Editorial by en_translator


For beginners

This problem asks you to track how much water remains in chronological order.

On adding water, the amount of water at that point can be computed based on the time passed since water is added last time, and the amount of water right after adding the water then. Specifically, when \(T\) unit time has passed since the water was remaining \(V\) liters, there is \(\max(0,V-T)\) liters of water; note that the amount never goes below \(0\).

By computing this in chronological order, we can track the amount of water.

Sample code (C++):

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

int main() {
    int n;
    cin >> n;
    int volume = 0;
    int now = 0; // Last water addition
    for (int i = 0; i < n; ++i) {
        int t, v;
        cin >> t >> v;
        volume -= (t - now); // Time passed since last addition
        volume = max(volume, 0);
        volume += v; // Add water
        now = t;
    }
    cout << volume << endl;
}

posted:
last update: