Comet OJ - Contest # 8 B [support city]

Comet OJ - Contest # 8 B support city

Direct analog \ (O (^ n-2) \) TLE, expand the calculation formula, \ (O (n-) \)

\[\sum_{i = 1}^n {(w_i - w_x)^2} = \sum_{i = 1}^n(w_i^2 - 2w_iw_x + w_x^2) = \sum_{i = 1}^n w_i^2 - 2w_x\sum_{i = 1}^n w_i + \sum_{i = 1}^n w_x^2\]

The $ \ sum_ {i = 1} ^ n w_i ^ 2 $ referred to as \ (SS \)

The \ (\ sum_ {i = 1 } ^ n w_i \) referred to as \ (S \)

The above-described expression abbreviated as: $ ss - 2w_xs + nw_x ^ 2 $

Code

#include <iostream>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
ll a[N],s,ss;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int n;
    cin >> n;
    for(int i = 0;i < n; ++i){
        cin >> a[i];
        s += a[i];
        ss += a[i] * a[i];
    }
    for(int i = 0;i < n; ++i){
        cout << ss - 2 * a[i] * s + n * a[i] * a[i] <<' ';
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/317zhang/p/11330671.html