Room for a Song
National Olympiad — Municipal round (2009 season)
Submission name: song.cpp
Problem summary
A song lasts n minutes and m seconds. Recording one second needs 16 KB. A disk has k MB free, with 1 MB = 1024 KB. Decide whether it fits; otherwise find the missing KB.
Input
Three integers n, m, k.
Output
YES if the song fits; otherwise NO on the first line and the missing number of KB on the second.
Constraints
1 ≤ n, m, k ≤ 1,000,000,000.
Worked example
An independently prepared example for this explanation.
Input
36 5 6
Output
NO 28496
Solution analysis
- Convert both quantities to the same unit. The song has 60n + m seconds, so it needs 16(60n + m) KB. The available capacity is 1024k KB.
- Compare the required and available capacities. Equality means the song fits. If required is larger, subtraction gives exactly the shortage.
- For 36 5 6, the duration is 2165 seconds, requiring 34640 KB. The disk has 6144 KB, leaving a shortage of 28496 KB.
Why it works
The conversion formulas give the exact required and available storage in KB. The song fits precisely when required ≤ available; otherwise required − available is the additional storage needed.
Watch out. Use 64-bit integers before multiplication. Do not assume m < 60: the published bounds allow larger values.
Commented C++17 solution
An independently written complete program with standard headers.
#include <iostream>
using namespace std;
int main() {
long long minutes, seconds, megabytes;
cin >> minutes >> seconds >> megabytes;
// Convert both capacities to KB before comparing them.
long long required = (60 * minutes + seconds) * 16;
long long available = megabytes * 1024;
if (required <= available) {
cout << "YES\n";
} else {
cout << "NO\n" << required - available << '\n';
}
}