【洛谷】P4995 跳跳!

One

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n;
vector<int> v;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    cin >> n;
    int cur;
    v.push_back(0);
    for (int i = 0; i < n; i++){
        cin >> cur;
        v.push_back(cur);
    }
    sort(v.begin(), v.end());
    long long sum = 0;
    int b = 0, e = v.size()-1;
    while (b < e){
        sum += (v[e]-v[b])*(v[e]-v[b]);
        b++;
        sum += (v[e]-v[b])*(v[e]-v[b]);
        e--;
    }
    cout << sum;
    return 0;
}

Two

#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <vector>
#include <functional>
using namespace std;
int n;
priority_queue<int, vector<int>, greater<int> > ql;
priority_queue<int, vector<int>, less<int> > qb;
vector<int> v;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    cin >> n;
    int cur;
    for (int i = 0; i < n; i++){
        cin >> cur;
        ql.push(cur);
        qb.push(cur);
    }
    v.push_back(0);
    for (int i = 0; i < n/2; i++){
            v.push_back(qb.top());
            v.push_back(ql.top());
            qb.pop(); ql.pop();

    }
    if (n%2) v.push_back(qb.top());
    long long sum = 0;
    for (int i = 1; i < v.size(); i++) sum += (v[i]-v[i-1])*(v[i]-v[i-1]);
    cout << sum;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值