abc180_b - Various Distances

Implementation.

Time complexity: $O(n)$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using vi = vector<int>;
tuple<ll, double, ll> solve(vi x)
{
    vector<ll> ans(3, 0);
    for (auto xi : x)
    {
        ans[0] += abs(xi);
        ans[1] += ll(xi) * xi;
        ans[2] = max(ans[2], ll(abs(xi)));
    }
    return {ans[0], sqrt(ans[1]), ans[2]};
}
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int n;
    cin >> n;
    vi x(n);
    for (auto &xi : x)
        cin >> xi;
    auto [ans0, ans1, ans2] = solve(x);
    cout << ans0 << endl;
    cout << setprecision(12) << fixed << ans1 << endl;
    cout << ans2 << endl;
    return 0;
}