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.
Don’t think about choosing all independently first. Think about the final common height .
If the final height is , then tower must use exactly . There is no freedom left after choosing .
Because every tower must be increased by at least , even the tallest tower must become taller. So if , then .
For a fixed , the needed is the largest increase:
To minimize , choose the smallest allowed final height, . Therefore the answer is
Suppose the final equal height is . Then for every tower , the chosen increase is forced:
So the whole game is really just choosing one number .
Now, every tower must be increased exactly once, and each increase must satisfy . In particular, even the tallest tower cannot stay the same. If
then the final height must satisfy
For a fixed final height , the smallest valid must cover the largest required increase:
The largest increase is needed by the shortest tower. Let
Then
So for fixed , we need
To minimize , choose the smallest possible , which is . Therefore:
That’s it. The only tiny trap is forgetting that the tallest tower must also increase by at least . If all towers are already equal, the answer is still , not .
For each test case, we just compute the minimum and maximum height.
Since , this is basically instant. The train will arrive before your CPU notices.
#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--) {
int n;
cin >> n;
int mn = INT_MAX, mx = INT_MIN;
for (int i = 0; i < n; i++) {
int h;
cin >> h;
mn = min(mn, h);
mx = max(mx, h);
}
cout << mx - mn + 1 << '\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--) {
int n;
cin >> n;
int mn = INT_MAX, mx = INT_MIN;
for (int i = 0; i < n; i++) {
int h;
cin >> h;
mn = min(mn, h);
mx = max(mx, h);
}
cout << mx - mn + 1 << '\n';
}
return 0;
}