Birthday
题目大意:(文末原题)
给出n个整数,使两两之间差值尽量小围成一个环,输出这个环;
思路:
将n个数排列,使最大的在中间,往两端减少,就可满足题意;
(也可以直接隔一项输出,再从末尾开始隔一项输出)
代码:
#include <iostream> #include <algorithm> using namespace std; int main() { int n; cin >> n; int a[200], ans[200]; for(int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int b = 0, e = n - 1; for(int i = 0; i < n; i++) { if(i % 2) ans[e--] = a[i]; else ans[b++] = a[i]; } for(int i = 0; i < n; i++) { cout << ans[i] << " "; } return 0; }
原题:
题目:
Cowboy Vlad has a birthday today! There are nn children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standing next to each other, and it will be difficult for them to hold hands. Therefore, children want to stand in a circle so that the maximum difference between the growth of two neighboring children would be minimal possible.
Formally, let's number children from 1 to n in a circle order, that is, for every i child with number i will stand next to the child with number i+1, also the child with number 1 stands next to the child with number nn. Then we will call the discomfort of the circle the maximum absolute difference of heights of the children, who stand next to each other.
Please help children to find out how they should reorder themselves, so that the resulting discomfort is smallest possible.
输入:
The first line contains a single integer n (2≤n≤100) — the number of the children who came to the cowboy Vlad's birthday.
The second line contains integers a1,a2,…,an (1≤ai≤10^9) denoting heights of every child.
输出:
Print exactly nn integers — heights of the children in the order in which they should stand in a circle. You can start printing a circle with any child.
If there are multiple possible answers, print any of them.
样例:
Input:
5
2 1 1 3 2Output:
1 2 3 2 1
Input:
3
30 10 20Output:
10 20 30