Day 52 B. Sorted Adjacent Differences

本文介绍了一种算法,用于解决给定整数数组,通过重新排列元素来确保相邻元素差值按绝对值递减。题目要求找到一种可能的排列方式,即使数组中存在重复数字。博主提供了C++代码实现并给出了两个示例。

Problem
You have array of n numbers a1,a2,…,an.

Rearrange these numbers to satisfy |a1−a2|≤|a2−a3|≤…≤|an−1−an|, where |x| denotes absolute value of x. It’s always possible to find such rearrangement.

Note that all numbers in a are not necessarily different. In other words, some numbers of a may be same.

You have to answer independent t test cases.

Input
The first line contains a single integer t (1≤t≤10^4) — the number of test cases.

The first line of each test case contains single integer n (3≤n≤10^5) — the length of array a. It is guaranteed that the sum of values of n over all test cases in the input does not exceed 10^5.

The second line of each test case contains n integers a1,a2,…,an (−10 ^ 9≤ai≤10 ^ 9).

Output
For each test case, print the rearranged version of array a which satisfies given condition. If there are multiple valid rearrangements, print any of them.

Example
input
2
6
5 -2 4 8 6 5
4
8 1 4 2
output
5 5 4 6 8 -2
1 2 4 8

Note
In the first test case, after given rearrangement, |a1−a2|=0≤|a2−a3|=1≤|a3−a4|=2≤|a4−a5|=2≤|a5−a6|=10. There are other possible answers like “5 4 5 6 -2 8”.

In the second test case, after given rearrangement, |a1−a2|=1≤|a2−a3|=2≤|a3−a4|=4. There are other possible answers like “2 4 8 1”.

#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<stdio.h>
#include<limits.h>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define ll long long
using namespace std;
//按照新数组各元素之间差值排序 
//先排序 排序后从两头开始取 最后倒序输出
int a[100005];
int main()
{
	int t;
	int n;
	cin >> t;
	while (t--)
	{
		memset(a, 0, 100005);
		cin >> n;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
		}
		sort(a + 1, a + n + 1);
		vector<int>v;
		int x = 1;
		int y = n;
		while (x <= y)
		{
			v.push_back(a[y]);
			y--;
			if (x > y)
			{
				break;
			}
			v.push_back(a[x]);
			x++;
		}
		for (int i = n - 1; i >= 0; i--)
		{
			printf("%d ", v[i]);
		}
		cout << endl;
	}
	return 0;
}
### `.sorted()` 方法的功能与使用说明 在 Python 中,`.sorted()` 是一个内建函数,用于对所有可迭代的对象进行排序操作。与列表的 `.sort()` 方法不同,`.sorted()` 不会修改原始对象,而是返回一个新的排序后的列表。这种特性使得 `.sorted()` 在需要保留原始数据不变的情况下非常有用[^2]。 #### `.sorted()` 的基本语法 ```python sorted(iterable, key=None, reverse=False) ``` - `iterable`:需要排序的可迭代对象,如列表、元组、字符串等。 - `key`:可选参数,指定一个函数,该函数用于生成排序的关键值。 - `reverse`:可选布尔值,若为 `True`,则按降序排序;默认为 `False`,按升序排序。 #### 基本使用示例 对一个简单的整数列表进行排序: ```python numbers = [5, 2, 3, 1, 4] sorted_numbers = sorted(numbers) print(sorted_numbers) # 输出: [1, 2, 3, 4, 5] ``` 原始列表保持不变: ```python print(numbers) # 输出: [5, 2, 3, 1, 4] ``` #### 按照自定义规则排序 可以使用 `key` 参数指定一个函数,用于生成排序依据。例如,对一个包含元组的列表,按照元组的第二个元素进行排序: ```python random = [(2, 2), (3, 4), (4, 1), (1, 3)] sorted_random = sorted(random, key=lambda x: x[1]) print(sorted_random) # 输出: [(4, 1), (2, 2), (1, 3), (3, 4)] ``` #### 降序排序 通过设置 `reverse=True`,可以实现降序排序: ```python numbers = [5, 2, 3, 1, 4] sorted_desc = sorted(numbers, reverse=True) print(sorted_desc) # 输出: [5, 4, 3, 2, 1] ``` #### `.sorted()` 与 `.sort()` 的区别 - `.sorted()` 返回一个新的排序后的列表,而 `.sort()` 会直接修改原始列表。 - `.sort()` 仅适用于列表,而 `.sorted()` 可用于任何可迭代对象,如元组、字符串、字典等。 例如,使用 `.sort()` 会改变原始列表: ```python aList = [5, 2, 3, 1, 4] aList.sort() print(aList) # 输出: [1, 2, 3, 4, 5] ``` 而使用 `.sorted()` 则不会: ```python numbers = [5, 2, 3, 1, 4] sorted_numbers = sorted(numbers) print(numbers) # 输出: [5, 2, 3, 1, 4] print(sorted_numbers) # 输出: [1, 2, 3, 4, 5] ``` #### 处理复杂数据类型 `.sorted()` 可以处理更复杂的数据结构,例如字典列表。可以通过 `key` 参数指定排序依据: ```python data = [ {'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}, {'name': 'Charlie', 'age': 35} ] sorted_data = sorted(data, key=lambda x: x['age']) print(sorted_data) # 输出: [{'name': 'Bob', 'age': 25}, {'name': 'Alice', 'age': 30}, {'name': 'Charlie', 'age': 35}] ``` #### 性能考虑 虽然 `.sorted()` 提供了便利的排序方式,但在处理非常大的数据集时,需要注意其性能。由于 `.sorted()` 会创建一个新的列表,因此在内存使用上可能不如原地排序的 `.sort()` 高效。但在大多数应用场景中,这种差异可以忽略不计。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值