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. xrandr

    ubuntu 外接显示器 xrandr常用命令(这里的VGA与LVDS分别换成第1步中的设备名,如VGA1.LVDS1): xrandr --output VGA --same-as LVDS --a ...

  2. vba功能语句

    VBA语句集(第1辑) 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明Option Private Module '标记模块为私有,仅对同一工程中其它模块有用,在宏 ...

  3. WEB学习-CSS盒模型

    盒子的区域 一个盒子中主要的属性就5个:width.height.padding.border.margin. width是“宽度”的意思,CSS中width指的是内容的宽度,而不是盒子的宽度. he ...

  4. Maven实现多个项目关联自动化构建(maven-invoker-plugin插件的使用)

    以下内容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_build_automation.html: 注意: ...

  5. 你还在为移动端选择器picker插件而捉急吗?

    http://www.cnblogs.com/jingh/p/6381079.html 开题:得益于项目的上线,现在终于有时间来写一点点的东西,虽然很浅显,但是我感觉每经历一次项目,我就学到了很多的东 ...

  6. linux 配置maven环境变量

    vi /etc/profile 按照如下样例编辑环境变量. 编辑之后记得使用source /etc/profile命令是改动生效. 5.验证结果 在任意路径下执行mvn -version验证命令是否有 ...

  7. apk解包——修改后缀为zip

    将apk修改后缀为zip后,可直接解包查看资源文件

  8. Java多线程面试题归纳

    1.多线程有哪几种实现方法?举个样例说明下线程的同步. (1)Java多线程有两种实现方式:继承Thread类和实现Runnable接口,Thread就是实现了Runnable接口. 两个最简单的线程 ...

  9. 读书笔记-2java虚拟机的可达性算法与finalize方法

    JAVA通过可达性分析算法来确定堆中哪些对象是应该被回收的. 非常多人包含我曾经在不了解的时候总以为是通过引用计数器来推断某个对象是否应该被回收可是后来想了想包含查阅一些资料发现不是这种.由于假设採用 ...

  10. 值得收藏的45个Python优质资源(附链接)

    REST API:使用 Python,Flask,Flask-RESTful 和 Flask-SQLAlchemy 构建专业的 REST API https://www.udemy.com/rest- ...