UVa11158 - Elegant Permuted Sum

本文介绍了一种通过贪心策略解决的问题:给定一组整数,寻找这些整数的一种排列方式,使得相邻元素间的绝对差之和最大化。文章提供了一个具体的实现方案,并附带样例输入输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E

Next Generation Contest 3

Time Limit: 2 seconds

Elegant Permuted Sum

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.

Sample Input

Output for Sample Input

3
4 4 2 1 5
4 1 1 1 1
2 10 1
Case 1: 10
Case 2: 0
Case 3: 9

 

Problem Setter: Sohel Hafiz
Special Thanks: Jane Alam Jan


题目意思很简单。


贪心的策略就是先排序,将最大的和最小的放在中间,然后不断地往两侧放,每次取差值最大的数放。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 60;
int n,num[maxn];
int main(){

    int ncase,T=1;
    cin >> ncase;
    while(ncase--){
        cin >> n;
        for(int i = 0; i < n; i++)   scanf("%d",&num[i]);
        printf("Case %d: ",T++);
        sort(num,num+n);
        int ans = num[n-1]-num[0];
        int lft = 1,rgt = n-2;
        int lftval = num[0],rgtval = num[n-1];
        while(lft <= rgt){
            int tmp = -1,cur;
            if(abs(lftval-num[lft])>tmp){
                tmp = abs(lftval-num[lft]);
                cur = 0;
            }
            if(abs(lftval-num[rgt])>tmp){
                tmp = abs(lftval-num[rgt]);
                cur = 1;
            }
            if(abs(rgtval-num[lft])>tmp){
                tmp = abs(rgtval-num[lft]);
                cur = 2;
            }
            if(abs(rgtval-num[rgt])>tmp){
                tmp = abs(rgtval-num[rgt]);
                cur = 3;
            }
            ans += tmp;
            if(cur==0){
                lftval = num[lft];
                lft++;
            }
            if(cur==1){
                lftval = num[rgt];
                rgt--;
            }
            if(cur==2){
                rgtval = num[lft];
                lft++;
            }
            if(cur==3){
                rgtval = num[rgt];
                rgt--;
            }

        }
        printf("%d\n",ans);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值