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!
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).
Print minimum possible total instability.
2 1 2 3 4
1
4 1 3 4 6 3 4 100 200
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;
}

本文介绍了一种算法,用于解决2*n个人使用n-1个双人及2个单人游艇时,如何分配以使游艇整体不稳定性最小的问题。通过遍历所有可能的单人游艇乘坐者组合并计算剩余双人游艇的不稳定性来找到最优解。

604

被折叠的 条评论
为什么被折叠?



