Codeforces Round 979 (Div. 2) A. A Gift From Orangutan

题目链接:题目

大意:

排列一串数字,使得对于所有 i i i直到 i i i的最大最小值之差的和最大。

思路:

首先对于 i = 1 i=1 i=1最大最小值相等,肯定为零,而我们把最大和最小的放在前两个,因为极差一定是最大的,结果就是n-1个极差。

代码:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define MOD 1000000007
#define fi first
#define se second
#define pii pair<int, int>
#define vec vector

void solve(){   
    int n;
    cin >> n;
    vec<int> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    int mx = *max_element(a.begin(), a.end()), mn = *min_element(a.begin(), a.end());
    int ans = 0;
    for(int i = 1; i < n; i++){
        ans += mx - mn;
    }
    cout << ans << '\n';
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值