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.
Look at how changes when you move from to . Absolute values are annoying, but their slopes are very structured.
For one fixed term , it is linear everywhere except around . Linear functions vanish under a second difference.
Try computing for . Every disappears except one.
For interior positions, the identity is So are basically handed to you. Absolute cinema, no overthinking required.
After recovering the interior values, use the endpoints of : contains no , and contains no . The remaining residuals determine and respectively.
Research checked: the official Codeforces Round 1080 editorial/code at https://codeforces.com/blog/entry/151174, the problem statement at https://codeforces.com/contest/2195/problem/D, and a matching reputable video explanation at https://www.youtube.com/watch?v=eUSOiul0UmA all point to the same intended idea: use discrete second differences, then recover the two endpoints from boundary equations.
We are given
The trap is thinking we need to solve a whole linear system. We technically could, but that would be bringing a forklift to move a chair. The function has a kink exactly at , and that kink is the whole problem.
Consider the second difference
for an interior position .
For a fixed index , look at
If , then around the function is linear or the values still cancel out. In all such cases, this expression is .
If , then it becomes
Therefore all terms vanish except the one with , giving
So for every interior index,
This recovers in linear time.
Now we only need and .
Use :
The term for is zero, so after subtracting the known interior contribution, the remainder is
Thus
Similarly, use :
The term for is zero. After subtracting the known interior contribution, the remainder is
so
The case also works automatically: there are no interior values, so and .
All arithmetic must use long long, because can be as large as . The final answers fit in , but intermediate values do not care about your feelings.
Complexity:
per test case, and
over all tests. Memory usage is for the input/output arrays.
#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<ll> f(n + 1), a(n + 1);
for (int i = 1; i <= n; i++) cin >> f[i];
for (int i = 2; i <= n - 1; i++) {
a[i] = (f[i + 1] - 2 * f[i] + f[i - 1]) / 2;
}
ll remLast = f[n];
for (int i = 2; i <= n - 1; i++) {
remLast -= a[i] * (n - i);
}
a[1] = remLast / (n - 1);
ll remFirst = f[1];
for (int i = 2; i <= n - 1; i++) {
remFirst -= a[i] * (i - 1);
}
a[n] = remFirst / (n - 1);
for (int i = 1; i <= n; i++) {
if (i > 1) cout << ' ';
cout << a[i];
}
cout << '\n';
}
}#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<ll> f(n + 1), a(n + 1);
for (int i = 1; i <= n; i++) cin >> f[i];
for (int i = 2; i <= n - 1; i++) {
a[i] = (f[i + 1] - 2 * f[i] + f[i - 1]) / 2;
}
ll remLast = f[n];
for (int i = 2; i <= n - 1; i++) {
remLast -= a[i] * (n - i);
}
a[1] = remLast / (n - 1);
ll remFirst = f[1];
for (int i = 2; i <= n - 1; i++) {
remFirst -= a[i] * (i - 1);
}
a[n] = remFirst / (n - 1);
for (int i = 1; i <= n; i++) {
if (i > 1) cout << ' ';
cout << a[i];
}
cout << '\n';
}
}