Official
B - Humidifier 1 Editorial by en_translator
For beginners
- If you are new to learning programming and do not know where to start, please try Problem A "Welcome to AtCoder" from practice contest. There you can find a sample code for each language.
- Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in "AtCoder Beginners Selection".
- 「C++入門 AtCoder Programming Guide for beginners (APG4b)」 is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too.
- 「Python入門 AtCoder Programming Guide for beginners (APG4bPython)」 is a Python tutorial for competitive programmers. Again, this is only in Japanese.
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: