Description
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.
Sample Input
21 2 3 4
Sample Output
1
题解
在长度为$2*n$的数列中挑出两个数,使剩下的数两两配对差值和最小。
如果不挑出,贪心的思想,显然按原数组排序就可以找到。
考虑$n$很小,我们暴力枚举挑出的数,把剩下的排序即可。
I think there is no need to tell you how to solve this problem...
//It is made by Awson on 2017.9.30 #include <set> #include <map> #include <cmath> #include <ctime> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define Min(a, b) ((a) < (b) ? (a) : (b)) #define Max(a, b) ((a) > (b) ? (a) : (b)) using namespace std; void read(int &x) { ; ); ch = getchar()); ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar()); x *= -*flag; } ], n; ]; void work() { read(n); n <<= ; ; ; i <= n; i++) read(w[i]); ; i < n; i++) ; j <= n; j++) { ; ; k <= n; k++) if (k!=i && k!= j) sorted[++top] = w[k]; sort(sorted+, sorted++top); ; ; i <= top; i += ) cnt += sorted[i+]-sorted[i]; ans = Min(ans, cnt); } printf("%d\n", ans); } int main() { work(); ; }