CF 863B Kayaking【STL+暴力】

本文介绍了一个算法问题,旨在通过合理分配皮划艇上的人员来最小化整个旅行中皮划艇的不稳定性。该算法适用于2n个人员和n-1个双人皮划艇及2个单人皮划艇的情况,通过对所有可能的组合进行遍历找到最优解。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值