Topic link: https://www.nowcoder.com/acm/contest/86/J After
analyzing and
seeing this question, I immediately thought of a construction method, first put the largest, then the smallest, and then zoom in the next largest and second Small, I tried the following example, I can pass it, and it feels extremely correct. I typed the code without proof, and submitted it to AC, and it worked very quickly.Later found that it is not very proving correct
Code:
#include <iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[100010];
int p[100010];
int main()
{
ios::sync_with_stdio(false);
int n;cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n);
int pn=0;
for(int i=n-1,j=0;j<=i;){
p[pn++]=a[i--];
p[pn++]=a[j++];
}
// for(int i=0;i<n;i++){
// cout<<p[i]<<" ";
// }
// cout<<endl;
long long ans=0;
ans+=abs(p[0]-p[n-1]);
for(int i=1;i<n;i++){
ans+=abs(p[i]-p[i-1]);
}
cout<<ans<<endl;
//cout << "Hello world!" << endl;
return 0;
}