Editorial for Nam châm


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Gọi ~S~ là tổng khoảng cách giữa mọi cặp điểm ban đầu. Việc thực hiện phép toán trên hai nam châm ~i~, ~j~ với tham số ~t~ sẽ làm giảm ~S~ đi ~2(j - i - 1) \times t + 2t~. Do đó, cách thực hiện phép toán tối ưu là luôn chọn hai nam châm ~i~, ~j~ liên tiếp nhau, khi đó ~S~ sẽ giảm đi ~2d~. Tổng khoảng cách cuối cùng (khi không thể thực hiện thêm phép toán nào) luôn bằng ~0~. Do đó, tổng điểm tối đa nhận được sẽ là ~S/2~.

#include <bits/stdc++.h>

using namespace std;

void solve() {
    int n; cin >> n;
    vector<long long> x(n);
    for(int i = 0; i < n; ++i) cin >> x[i];

    long long sum = 0;
    for(int i = 1; i < n; ++i) {
        sum += (x[i] - x[i-1]) * i * (n - i);
    }

    cout << sum / 2.0 << endl;
}

int main() {
    cout << setprecision(30);

    int t; cin >> t;
    while (t--) solve();

    return 0;
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.