codeforces 863B 之 Kayaking

Kayaking
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.

Now the party is ready to start its journey, but firstly they have to choose kayaks. There are n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.

Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.

Help the party to determine minimum possible total instability!

Input

The first line contains one number n (2 ≤ n ≤ 50).

The second line contains n integer numbers w1w2, ..., w2n, where wi is weight of person i (1 ≤ wi ≤ 1000).

Output

Print minimum possible total instability.

Examples
input
2
1 2 3 4
output
1
input
4
1 3 4 6 3 4 100 200
output

5


题意:有2*n个人去玩游艇,有n-1个双人游艇,2个单人游艇,单人游艇是安全的,双人游艇的不安全指数是两个人的体重差,求最小的不安全指数


分析:暴力取出两个人坐在单人游艇上,更新最小值


AC代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn=2*50+10;
const int inf=0x3f3f3f3f;

int a[maxn];
int b[maxn];

int main()
{
    int n;
    int ans;
    while(cin>>n)
    {
        ans=inf;
        n=n<<1;
        for(int i=0;i<n;i++) cin>>a[i];

        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
                if(i==j) continue;
                memset(b,0,sizeof(b));
                int num=0;
                for(int k=0;k<n;k++)
                {
                    if(k!=i && k!=j) b[num++]=a[k];
                }
                sort(b,b+num);
                int sum=0;
                for(int k=0;k<num;k+=2)
                    sum+=b[k+1]-b[k];
                ans=min(ans,sum);
            }
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值