CF 863B Kayaking【STL+暴力】

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 2·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 2·n integer numbers w1, w2, …, 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

数据范围小,直接暴力

#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int a[110];
int b[11000];
int main()
{
    int n;
    cin >> n;
    n <<= 1;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    int ans = inf;
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            vector<int> v;
            for (int k = 0; k < n; k++)
            {
                if (i != k&&j != k)
                {
                    v.push_back(a[k]);
                }
            }
            sort(v.begin(), v.end());
            int temp = 0;
            for (int k = 1; k<int(v.size()); k += 2)
            {
               temp += abs(v[k] - v[k - 1]);
            }
            ans = min(ans, temp);
        }
    }
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值