标签:
6
12 11 24 17 12 24
60
//贪心,取两头,怎么证明我不懂 #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #define ll long long using namespace std; ll n,a[10005],b[10005],sum; int main(){ cin>>n; for(int i = 1;i <= n;i++) scanf("%lld",&a[i]); sort(a+1,a+1+n); int l = 1,r = n,opt = 0; for(int i = 1;i <= n;i++){ if(!opt) b[i] = a[l++]; else b[i] = a[r--]; opt ^= 1; } b[0] = b[n]; for(int i = 1;i <= n;i++){ if(b[i] > b[i-1]) sum += b[i] - b[i-1]; else sum += b[i-1] - b[i]; } cout<<sum; return 0; }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/5791423.html