A - 二人の水筒 / Two Water Bottles Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
Determine whether the total amount of water the two people have is at least the required amount, \(C\) milliliters. Output Yes if it is sufficient, and No if it is not.
Analysis
Since the total amount of water between the two people is \(A+B\) milliliters, the condition for having enough water is:
\(A+B \geq C\)
It is sufficient to check this condition directly, without any loops. Simple addition and comparison are all that is needed to solve the problem well within the time limit.
In the presented code, the value obtained by subtracting the two people’s amounts of water from the required amount is used:
\(\mathrm{remaining}=C-A-B\)
- If \(\mathrm{remaining} \leq 0\), the required amount is secured, so output
Yes. - If \(\mathrm{remaining} > 0\), there is a shortage of water by that amount, so output
No.
For example, when \(A=300\), \(B=200\), and \(C=500\), we have \(\mathrm{remaining}=0\). Having exactly the required amount is also considered “sufficient”. Note that if you check for \(\mathrm{remaining}<0\), it will produce a wrong answer when the amounts are exactly equal.
Algorithm
- Read \(A,B,C\).
- Calculate \(\mathrm{remaining}=C-A-B\).
- If \(\mathrm{remaining} \leq 0\), output
Yes; otherwise, outputNo.
Complexity
- Time complexity: \(O(1)\)
- Space complexity: \(O(1)\)
Only a constant number of calculations and comparisons are performed, and a constant number of variables are used.
Implementation Notes
Since having the exact required amount of water also results in
Yes, use<=for the comparison.Under the given constraints, all input values and calculation results fit within the standard
inttype in C++.The output is case-sensitive, so make sure to output
YesorNowith the correct casing.Source Code
// 問題文から独立に実装: 必要量から二人分を引いて残りが 0 以下か
#include <iostream>
int main() {
int a, b, c; std::cin >> a >> b >> c;
int remaining = c - a - b;
if (remaining <= 0) std::cout << "Yes" << std::endl;
else std::cout << "No" << std::endl;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: