Special Thanks: Jane Alam Jan
*At moment in University of Texas at San Antonio - USA

You will be given n integers A1A2A3...An. Find a permutation of these n integers so that summation of the absolute differences between adjacent elements is maximized.

Suppose n = 4 and the given integers are 4 2 1 5. The permutation 2 5 1 4 yields the maximum summation. For this permutation sum = abs(2-5) + abs(5-1) + abs(1-4) = 3+4+3 = 10.

Of all the 24 permutations, you won’t get any summation whose value exceeds 10. We will call this value, 10, the elegant permuted sum.

Input

The first line of input is an integer T (T < 100) that represents the number of test cases. Each case consists of a line that starts with n (1 < n < 51) followed by n non-negative integers separated by a single space. None of the elements of the given permutation will exceed 1000.

Output

For each case, output the case number followed by the elegant permuted summation.

Example

Input:
3
4 4 2 1 5
4 1 1 1 1
2 10 1 Output:
Case 1: 10
Case 2: 0
Case 3: 9
题意:给定组数,现在要你排序,使得排序后所有相邻两个数的差的和最大。
思路:排序,选择第一个点为左起点pos1,最后一个点为右起点pos2,然后以左起点和右起点贪心选择最大路径。
应用模型:X轴上有N个点,现在要你选择一个点作为起点,然后一个个的访问未访问过的点,问访问完所有点的最小距离是多少。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T,N,i,Case=,a[],ans;;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d",&a[i]);
sort(a+,a+N+);
ans=a[N]-a[];
int pos1=,pos2=N,L=,R=N-;
while(L<=R){
int tmp=,t[];t[]=-;
t[]=abs(a[pos1]-a[L]);
t[]=abs(a[pos2]-a[L]);
t[]=abs(a[pos1]-a[R]);
t[]=abs(a[pos2]-a[R]);
for(i=;i<=;i++) if(t[i]>t[tmp]) tmp=i;
ans+=t[tmp];
if(tmp==) pos1=L,L++;
if(tmp==) pos2=L,L++;
if(tmp==) pos1=R,R--;
if(tmp==) pos2=R,R--;
}
printf("Case %d: %d\n",++Case,ans);
}
return ;
}

SPOJ:Elegant Permuted Sum(贪心)的更多相关文章

  1. HDU 5813 Elegant Construction (贪心)

    Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...

  2. 数位DP:SPOJ KPSUM - The Sum

    KPSUM - The Sum One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that ...

  3. SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  4. SPOJ LCMSUM - LCM Sum

    题意是求: $\sum_{i = 1}^{n}lcm(i, n)$ $= \sum_{i = 1}^{n}\frac{ni}{gcd(i, n)}$ $= n\sum_{i = 1}^{n}\frac ...

  5. SPOJ:PATHETIC STRINGS(分配问题&贪心)

    Problem statement: A string is said to be “PATHETIC” if all the characters in it are repeated the sa ...

  6. SPOJ:The Next Palindrome(贪心&思维)

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  7. HDU4825 Xor Sum(贪心+Trie树)

    Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeu ...

  8. Constant Palindrome Sum(贪心*RMQ)

    传送门 怎么说呢,想了几个小时没做出来实在可惜. \(\color{Red}{首先肯定想到暴力嘛!但是x定值有那么多值可以取,怎么办呢?}\) 但是题目中有一个很关键的条件 \[a[i]>=1\ ...

  9. Codeforces Global Round 8 D. AND, OR and square sum (贪心,位运算)

    题意:有\(n\)个数,选择某一对数使二者分别\(or\)和\(and\)得到两个新值,求操作后所有数平方和的最大值. 题解:不难发现每次操作后,两个数的二进制表示下的\(1\)的个数总是不变的,所以 ...

随机推荐

  1. CCPC-Wannafly Winter Camp Day1 (Div2, online mirror) A,B,C,E,F,I,J

    https://www.zhixincode.com/contest/7/problems A题 分类讨论 当B有点需要经过时 穿梭的花费肯定为2*k,也可以发现,我们要找到包含所有需要经过的点(不含 ...

  2. python socket非阻塞及python队列Queue

    一. python非阻塞编程的settimeout与setblocking+select 原文:www.th7.cn/Program/Python/201406/214922.shtml 侧面认证Py ...

  3. python和linux的环境设置/PATH

    一.python的环境设置 1.输出path列表: pi@raspberrypi:~$ pythonPython 3.4.0 (default, Jul 1 2014, 09:37:01) [GCC ...

  4. Java集合——遍历集合元素并修改

    Java集合——遍历集合元素并修改 摘要:本文主要总结了遍历集合的方式,以及在遍历时修改集合要注意的问题. 遍历Collection 对List和Set的遍历,有四种方式,下面以ArrayList为例 ...

  5. Material Theme

    Material Theme提供了一下功能: 1.系统widgets可以设置调色板 2.系统widgets的触摸反馈 3.Activity过渡动画 你可以根据你品牌的色彩来定义Material The ...

  6. C语言printf()函数具体解释和安全隐患

    一.问题描写叙述 二.进一步说明 请细致注意看,有例如以下奇怪的现象 int a=5; floatx=a; //这里转换是没有问题的.%f打印x是 5.000000 printf("%d\n ...

  7. AngularJs概述

  8. HDU 2578 Dating with girls(1) [补7-26]

    Dating with girls(1) Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. [游戏]L4D求生之路官方比赛地图修补完好说明

    游戏模式:L4D求生之路4356(1.0.2.1)药抗比赛模式 更新日期:2015.06.04 -----毫不留情01----- 1.开局补给手枪 -----毫不留情02----- 1.开局补给手枪 ...

  10. numpy - 数组索引

    numpy 数组索引 一.单个元素索引 一维数组索引 >>> x = np.arange(10) >>> x[2] 2 >>> x[-2] 8 二 ...