题目链接:题目
大意:
排列一串数字,使得对于所有 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;
}