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. druid 的应用(密码加密),logback的应用

    参考博客:https://github.com/yjmyzz/spring-mybatis-multidatasourcehttp://www.cnblogs.com/dream-to-pku/p/6 ...

  2. Java 并发编程中的 Executor 框架与线程池

    Java 5 开始引入 Conccurent 软件包,提供完备的并发能力,对线程池有了更好的支持.其中,Executor 框架是最值得称道的. Executor框架是指java 5中引入的一系列并发库 ...

  3. (8)C#连sqlserver

    str="Data Source=ip; Network Library=DBMSSOCN; Initial Catalog=数据库; User ID=sa; Password=xx&quo ...

  4. [bzoj5457]城市_dsu on tree

    bzoj 5457 城市 题目大意 给定一棵以\(1\)为根的\(n\)个节点的有根树. 每个节点有一个民族和该民族在当前节点的人数. 有\(n\)个询问,第\(i\)个询问是求以\(i\)为根的子树 ...

  5. Hadoop 学习 HDFS

    1.HDFS的设计 HDFS是什么:HDFS即Hadoop分布式文件系统(Hadoop Distributed Filesystem),以流式数据访问模式来存储超大文件,运行于商用硬件集群上,是管理网 ...

  6. DICOM医学图像显示算法改进与实现——LUT

    引言 随着Ul(超声成像).CT(计算机断层成像).MRI(核磁共振成像).CR(计算机X线成像).电子内窥镜.盯(正电子发射断层成像)和MI(分子影像)等医学影像设备不断涌现,利用计算机对医学影像设 ...

  7. palindrome-partitioning I&II——回文切割、深度遍历

    I: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  8. Elasticsearch 学习笔记 Elasticsearch及Elasticsearch head安装配置

    一.安装与配置 1.到官网下载Elasticsearch,https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6. ...

  9. Linux下VLAN功能的实现 (转)

    1.Linux网络栈下两层实现 1.1简介     VLAN是网络栈的一个附加功能,且位于下两层.首先来学习Linux中网络栈下两层的实现,再去看如何把VLAN这个功能附加上去.下两层涉及到具体的硬件 ...

  10. OpenCV2.3.1在CentOS6.5下的安装

    安装的linux版本号是centos6.5.选择的是opencv2.3.1.不是非常新的版本号. 由于在安装opencv2.4.9的时候.make的过程中出现了问题. 一:安装依赖包 依赖包用yum安 ...