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.
First compute the time if Blackslex visits every floor: . Skipping one floor only changes the parts directly next to that floor.
If you remove the first or last element, you just delete one edge from the path. So the savings are or .
For a middle index , the old contribution around it is . After deleting it, that becomes .
So deleting middle index saves . This is never negative by triangle inequality, because math is occasionally merciful.
Try every possible deleted index, take the maximum savings, and output . This is linear time per test case.
Idea
The elevator route is basically a path through the array. The cost is the sum of edge lengths between consecutive elements:
We may delete at most one element. Since deleting an endpoint removes one positive-or-zero edge, and deleting a middle element cannot increase the cost by triangle inequality, deleting one element is always at least as good as deleting none. So we can just find the best single deletion.
What changes when we delete something?
Most of the route does not care. If you delete , only the neighbors around are affected.
If , the edge disappears. The saving is:
If , the edge disappears. The saving is:
For a middle index , the old local cost was:
After deleting , the path jumps directly from to , so the new local cost is:
Therefore the saving is:
This is the entire problem. No DP shrine required, despite the tag trying to look important.
Algorithm
best with the better endpoint deletion saving.best.Why this works
The total cost is a sum of adjacent differences. Deleting one element only affects the adjacent edges touching that element. Every other edge remains exactly the same.
So instead of recomputing the full array cost after every possible deletion, we compute how much each deletion saves compared to the original total . The best deletion is simply the one with maximum savings.
For middle elements, we subtract the two old edges and add the new shortcut edge. For endpoints, there is no shortcut edge, just one removed edge.
The statement says to output a real number, but all values are integers, so printing an integer is perfectly fine.
Complexity
Each test case is processed in time and memory for the array. The total over all tests is bounded, so this easily fits.
#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;
vector<int> a(n);
for (int &x : a) cin >> x;
ll total = 0;
for (int i = 0; i + 1 < n; i++) {
total += abs(a[i] - a[i + 1]);
}
ll bestSave = max(abs(a[0] - a[1]), abs(a[n - 2] - a[n - 1]));
for (int i = 1; i + 1 < n; i++) {
ll oldCost = abs(a[i - 1] - a[i]) + abs(a[i] - a[i + 1]);
ll newCost = abs(a[i - 1] - a[i + 1]);
bestSave = max(bestSave, oldCost - newCost);
}
cout << total - bestSave << '\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;
vector<int> a(n);
for (int &x : a) cin >> x;
ll total = 0;
for (int i = 0; i + 1 < n; i++) {
total += abs(a[i] - a[i + 1]);
}
ll bestSave = max(abs(a[0] - a[1]), abs(a[n - 2] - a[n - 1]));
for (int i = 1; i + 1 < n; i++) {
ll oldCost = abs(a[i - 1] - a[i]) + abs(a[i] - a[i + 1]);
ll newCost = abs(a[i - 1] - a[i + 1]);
bestSave = max(bestSave, oldCost - newCost);
}
cout << total - bestSave << '\n';
}
return 0;
}