P4995 跳跳题解

题目传递门

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 310;
int a[N];
int n;
LL res;
int h, t, cnt;

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);
    //策略:从最低直接跳到最高,然后跳到次低,跳到次高...
    t = n;
    //双指针开始
    while (h < t) {
        cnt++;
        res += pow(a[h] - a[t], 2);
        if (cnt % 2 > 0)h++;
        else t--;
    }
    //输出大吉
    cout << res << endl;
    return 0;
}
posted @ 2021-07-21 08:50  糖豆爸爸  阅读(39)  评论(0编辑  收藏  举报
Live2D