Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Inside one tower, not every box is equally stressed. The bottom box has it worst.
If a tower has height , the bottom box has boxes above it, so it carries total weight .
A tower of height is valid exactly when . Solve this inequality for the largest possible .
The maximum valid tower height is . No tower can have more than boxes, and every tower with at most boxes is fine.
Now the problem is just: split identical boxes into groups of size at most , using as few groups as possible. That answer is .
Research check: the official Codeforces statement, the official contest editorial, and a public C++ solution on GitHub all reduce the problem to the same formula: compute the maximum tower height, then do ceiling division.
Consider a single tower with boxes.
For a box, the only thing that matters is how many boxes are above it. Since every box has weight , if there are boxes above some box, then the load on it is .
The most loaded box is the bottom box, because it has all other boxes above it. So the whole tower is safe iff the bottom box is safe:
Solving for :
so the maximum number of boxes in one tower is
That is the whole trick. No DP, no greedy ordering, no weird box identity nonsense. The boxes are identical, so once we know the maximum valid tower size, the rest is just packing items into bins of capacity .
Now we need the minimum number of towers. If we use towers, they can contain at most boxes total. Therefore we must have
which means
This lower bound is achievable: fill towers with boxes each, and put the leftover boxes into one final tower if needed. Since the leftover is at most , that final tower is also valid. So the optimal answer is exactly
Implementation detail: integer ceiling division for positive integers is
using integer division.
Edge cases:
Complexity is time and memory. Tiny constraints, but the formula would survive much larger ones too.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, m, d;
cin >> n >> m >> d;
ll k = d / m + 1;
cout << (n + k - 1) / k << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, m, d;
cin >> n >> m >> d;
ll k = d / m + 1;
cout << (n + k - 1) / k << '\n';
}
return 0;
}