牛客day4 H Yet Another Origami Problem

题目链接

2024牛客暑期多小训练营4-H

题目描述

Fried-chicken hates origami problems! He always fails at solving origami problems that others can solve easily (for example, CF1966E and HDU6822). However, today Fried-chicken is the problem setter! It’s his turn to give contestants a very difficult origami problem!

Given an array a\textstyle aa of length n\textstyle nn, you can perform the following operations any number of times (possibly zero):

Choose an index p\textstyle pp, and then perform one of the following two operations:

  1. For all i\textstyle ii such that ai≤ap\textstyle a_i \leq a_pai​≤ap​, let ai←ai+2(ap−ai)\textstyle a_i \gets a_i + 2(a_p - a_i)ai​←ai​+2(ap​−ai​).

  2. For all i\textstyle ii such that ai≥ap\textstyle a_i \geq a_pai​≥ap​, let ai←ai−2(ai−ap)\textstyle a_i \gets a_i - 2(a_i - a_p)ai​←ai​−2(ai​−ap​).

For example, if the original array is [2,4,5,3]\textstyle [2,4,5,3][2,4,5,3] and you choose p=2\textstyle p=2p=2 and perform operation 1, the array will become [6,4,5,5]\textstyle [6,4,5,5][6,4,5,5].

Now, you want to minimize the range of the array through these operations. Recall that the range of an array is the difference between the maximum and minimum elements of the array.

输入描述:

 

Each test contains multiple test cases. The first line contains the number of test cases t\textstyle tt (1≤t≤5×105\textstyle 1 \leq t \leq 5 \times 10^51≤t≤5×105). The description of the test cases follows.

The first line of each test case contains an integer n\textstyle nn (1≤n≤105\textstyle 1 \leq n \leq 10^51≤n≤105), representing the length of the array.

The second line contains n\textstyle nn integers a1,a2,…,an\textstyle a_1, a_2, \ldots, a_na1​,a2​,…,an​ (0≤ai≤1016\textstyle 0 \leq a_i \leq 10^{16}0≤ai​≤1016), describing the elements of the array a\textstyle aa in the initial state.

It is guaranteed that the sum of n\textstyle nn over all test cases does not exceed 5×105\textstyle 5\times 10^55×105.

输出描述:

 

For each test case, output one integer on a single line, representing the minimum range of the array after any number of operations.

示例1

输入

3
4
2 4 5 3
3
1 2 100
1
10000

输出

1
1
0

题目大意

给两种操作:排序数组后在值域上翻折一个前缀或翻折一个后缀,问任意次操作能做到的最小极差

解题思路

先直接给出答案。将输入数组 排序,则答案为ans=gcd(a2-a1,a3-a2....an-an-1);

由题可知数据只有一种时答案只会出0,因此要特判

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 7;
int a[N];
void slove() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
	}
	sort(a + 1, a + 1 + n);
	if (n == 1) {
		cout << 0 << endl;
		return;
	}
	//根据样例很容易推出ans=gcd(a2-a1,a3-a2....an-an-1) 
	int s1 = a[2] - a[1];
	for (int i = 3; i <= n; i++) {
		s1 = __gcd(s1, a[i] - a[i - 1]);
	}
	cout << s1 << endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
		slove();
	return 0;
}

  • 15
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牛客 a卷2022年第四季度的华为题目中,要求考生设计一种高效的数据结构,能够支持以下几种操作: 1. 添加一个元素 2. 删除一个元素 3. 查找是否存在某个元素 4. 返回元素的总数 该数据结构要求满足空间复杂度较小、时间复杂度较低、能够快速地进行查找和修改等多种操作。 想要编写这样一种数据结构,我们可以参考许多已有的经典算法与数据结构,如二叉树、哈希表、红黑树等,通过综合利用它们的优点来实现这个问题的解决。 例如,我们可以通过哈希表来存储所有元素的值,并在每个哈希链表的元素中再使用红黑树来进行排序与查找。这样,我们既能够轻松地进行元素的添加和删除操作,也能够在查找较大数据范围和数量时保持较高的速度与效率。同时,由于使用了多个数据结构来协同完成这个问题,我们也能够在空间复杂度上适度地进行优化。 当然,在具体设计这个数据结构的过程中,我们还需要考虑一些实践中的细节问题,例如如何避免哈希冲突、如何处理数据丢失与被删除元素所占用的空间等问题,这都需要相应的算法与流程来进行处理。 总体来看,设计这种支持多种操作的高效数据结构,需要我们具备丰富的算法知识和编程实践能力,同时需要我们在具体处理问题时能够将多种算法和数据结构进行有效地结合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值