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. IOS开发之触摸背景关闭键盘的代码实现

    直接上代码: // 触摸背景,关闭键盘 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch ...

  2. 让Mac OS X下的终端像Linux那样拥有丰富多彩的颜色显示

    我们知道Linux下的命令行终端具有颜色回显功能,用ls命令查看目录或者文件,终端会以不同的颜色来区分:使用vim命令行编辑器打开脚本或其他源程序代码会以语法高亮模式显示.而Mac OS X下的终端却 ...

  3. What should do in Production

    Using Compose in production https://docs.docker.com/compose/production/

  4. Codeforces 665C Simple Strings【暴力,贪心】

    题目链接: http://codeforces.com/contest/665/problem/C 题意: 改变最少的字符,使得最终序列无相同的连续的字符. 分析: 对每一个与前一个字符相同的字符,枚 ...

  5. idea抛异常方式

    选中需要抛异常的行,按alt+enter或者ctrl+alt+t,然后上下键选择自己抛异常的方式即可,如下图:

  6. 【postgresql】postgresql中的between and以及日期的使用

    在postgresql中的between and操作符作用类似于,是包含边界的 a BETWEEN x AND y 等效于 a >= x AND a <= y 在postgresql中比较 ...

  7. People seldom do what they believe in. They do what is convenient, then repent.

    People seldom do what they believe in. They do what is convenient, then repent. 人们很少真正实践他们的理想.他们只做比较 ...

  8. cocos2d-x 3.0 引用第三方库 及编译成apk时android mk文件写法

    cocos2d-x 3.0 中.假设你须要使用CocosStudio.Extensions扩展库 等等.都须要自己手动加入. 加入过程例如以下:(比方说如今我要加入libExtensions,libC ...

  9. IO模型:同步、异步、阻塞、非阻塞

    前言: 在Linux的网络编程中,同步IO(synchronous IO).异步IO(asynchronous IO).阻塞IO(blocking IO).非阻塞IO(non-blocking IO) ...

  10. cin,和几个get函数的用法

    1.cin.get(字符变量名):用来接收字符 ch = cin.get(); cin.get(ch); 以上两者均可以 2.cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格 ...