首页 > 其他分享 >Equalize

Equalize

时间:2024-07-07 17:30:36浏览次数:1  
标签:leq number test Equalize permutation 序列 array

Equalize

题面翻译

有一个给定的长度为 \(n\) 的数列 \(a\),现在加上一个排列 \(b\),即 \(c_i=a_i+b_i\)。

现在求对于所有可能的 \(b\),\(c\) 中出现最多的数的出现次数的最大值。

translate by @UniGravity.

题目描述

Vasya has two hobbies — adding permutations $ ^{\dagger} $ to arrays and finding the most frequently occurring element. Recently, he found an array $ a $ and decided to find out the maximum number of elements equal to the same number in the array $ a $ that he can obtain after adding some permutation to the array $ a $ .

More formally, Vasya must choose exactly one permutation $ p_1, p_2, p_3, \ldots, p_n $ of length $ n $ , and then change the elements of the array $ a $ according to the rule $ a_i := a_i + p_i $ . After that, Vasya counts how many times each number occurs in the array $ a $ and takes the maximum of these values. You need to determine the maximum value he can obtain.

$ ^{\dagger} $ A permutation of length $ n $ is an array consisting of $ n $ distinct integers from $ 1 $ to $ n $ in arbitrary order. For example, $ [2,3,1,5,4] $ is a permutation, but $ [1,2,2] $ is not a permutation ( $ 2 $ appears twice in the array), and $ [1,3,4] $ is also not a permutation ( $ n=3 $ but there is $ 4 $ in the array).

输入格式

Each test consists of multiple test cases. The first line contains a single integer $ t $ ( $ 1 \leq t \leq 2 \cdot 10^4 $ ) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the length of the array $ a $ .

The second line of each test case contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \le a_i \le 10^9 $ ) — the elements of the array $ a $ .

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, output a single number — the maximum number of elements equal to the same number after the operation of adding a permutation.

样例 #1

样例输入 #1

7
2
1 2
4
7 1 4 1
3
103 102 104
5
1 101 1 100 1
5
1 10 100 1000 1
2
3 1
3
1000000000 999999997 999999999

样例输出 #1

2
2
3
2
1
1
2

提示

In the first test case, it is optimal to choose $ p = [2, 1] $ . Then after applying the operation, the array $ a $ will be $ [3, 3] $ , in which the number $ 3 $ occurs twice, so the answer is $ 2 $ .

In the second test case, one of the optimal options is $ p = [2, 3, 1, 4] $ . After applying the operation, the array $ a $ will be $ [9, 4, 5, 5] $ . Since the number $ 5 $ occurs twice, the answer is $ 2 $ .

解题思路

题意可理解为求出通过将任意不重复的小于等于\(n\)的数对原序列中的数据进行替换后数组中可以通过上述操作所获得的相同的元素的最大数量。

可以先进行试想,对于一个等差数列我们可以通过加上一个完全相反的数列,使得所有数都变为相同的数,满足了题目的要求。

因为序列不对替换顺序进行要求,我们可以打乱原数组的顺序,构造一个含有间断点的类似于等差数列的序列。也就是说,我们可以对原序列进行排序,然后取出中间任意一部分理想的子序列构造出一个含有部分相同项和缺少部分项的等差数列。不难发现,因为题目中进行相加的序列不可以出现重复的数值,所以如果原序列中出现了相同的数,不可能存在相加不同的数的操作使得原序列中的两个相同的数仍然相同的情况。所以原序列相同的数据仅仅可能只有其中的一个是有效的。我们就可以把我们构造出的序列中的相同项全部删除,得到一个缺失部分项的等差数列。

题目中没有说明所有\(\leq n\)的数都必须使用,也就是说,我们可以再次构建一个缺少项的等差数列,让缺少项的等差序列和上文中构建的序列相加,这个使得这个序列和刚才我们构建的序列满足我们试想中的情况。

所以,我们就可以先判断试想中的合法条件,然后推广到这个题目。

在试想的情况中,我们应用类似于题目中的条件,即相反的序列必须满足\(\leq n\)。由于没有重复项,并且序列是满足递增的(单调),如果最大项和最小项(也就是要进行判断合法的序列中差值最大、最难相等的两个数)在这个条件下可以成立,那么其它差值较小的元素也一定可以成立。

我们希望最大项和最小项相等,那么就要让最小项的加值尽可能大,让最大项的加值尽可能小。那么在保证合法的前提下使用构造的相反数列中的最小项1和最大项\(n\)。我们让最小值+n,最大值+1。如果存在$ max + 1 \leq min + n $的情况成立,那么其它项之间较小的插值也一定能通过这个相反序列满足。题目中我们所能够构造的数列仅仅是试想中两个互相相反的序列各去除了一些项所得到的,所以我们可以将这个条件直接应用到解题中。

我们可以使用双指针去枚举所有合法的情况然后取出最大值作为答案。也可以使用$ max + 1 \leq min + n $这个条件,枚举左端点,然后使用二分查找寻找所有合法的右端点,然后取出最大值(直接暴力时间复杂度过高会超时)。

(仅列举第一种方法)

#include<bits/stdc++.h>
using namespace std;
int t,n;
vector<long long>a(n);
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		vector<long long>a(n);
		for(int i=0;i<n-1;i++){
			scanf("%d ",&a[i]);
		}scanf("%d",&a[n-1]);
		sort(a.begin(),a.end());
		a.erase(unique(a.begin(), a.end()), a.end());
		int m = a.size(), res = 0;
		for (int l=0,r=0;r<m;r++){
			while(l<=r&&a[r]+1-a[l]>n)l++;
			if(l<=r&&a[r]+1-a[l]<=n)res=max(res,r-l+1);
		
		}
		printf("%d\n",res);
	}
	return 0;
}

标签:leq,number,test,Equalize,permutation,序列,array
From: https://www.cnblogs.com/haihaichibaola/p/18288733

相关文章

  • D. Divide and Equalize
    题解我们只需要将每个数拆成质因数相乘的形式,然后对每个质因数累加,最后观察每个质因数出现的次数是不是数组长度的整数倍即可。code #include<bits/stdc++.h>usingnamespacestd;constintN=1e4+5;inta[N];map<int,int>map1;boolss(intm){for(inti=2;i<......
  • Codeforces 1037C Equalize 题解
    题目描述给定两个长度为$n$的$01$序列$a,b$。每次可以执行如下操作:在$a$中选择一个位置$p$,将$a_p$变为$1-a_p$,代价是$1$。在$a$中选择两个位置$p,q$,将$a_p$和$a_q$互换,代价是$\lvertq-p\rvert$。问最少需要多少代价才能将$a$变成$b$。题目分析......
  • CF999D Equalize the Remainders 题解
    题意给定一个长度为\(n\)的序列和一个模数\(m\),记\(c_i\)表示\(\bmodm\)后的结果为\(i\)的数的个数。现在可以使每个数增加\(1\),请问最少要操作多少次才能使所有\(c_i=\frac{n}{m}\)。并输出最后的序列。First.如何最小化操作次数由于每次操作会使\(c_{a_i\bm......
  • Equalize
    这道题目的官方题解讲的就很好但是这个构造方法确实比较好,我当时想到了排序+去重,但是最后判断的时候利用了贪心,对任何一个剩下的元素\(a\),最后如果他要参与贡献的话,这个众数必须是\([a+1,a+n]\)中的一个数,而由于\(a\)从小到大排了序的,我们依次考虑的话,显然是每个\(a\)都选择\(a+......
  • Codeforces Round 924 (Div. 2)B. Equalize(思维+双指针)
    目录题面链接题意题解代码题面链接B.Equalize题意给一个数组\(a\),然后让你给这个数组加上一个排列,求出现最多的次数题解赛时没过不应该。最开始很容易想到要去重,因为重复的元素对于答案是没有贡献的。去重后排序。,然后维护一个极差小于n-1的区间,,区间长度就是可能的答案......
  • Educational Codeforces Round 159 (Rated for Div. 2) C. Insert and Equalize (贪心+
    EducationalCodeforcesRound159(RatedforDiv.2)C.InsertandEqualize思路:首先对\(a\)进行排序,然后对所有差值取gcd,获得可用的最大因子\(gc\),答案有两种情况:一种是\(a_{n+1}\)在$a_1\(~\)a_n$范围内,这时要获得最大的位置一种情况是$a_1\(~\)a_n$......
  • C. Insert and Equalize
    原题链接导论1.数列末尾插入一个没有在数列中出现过的数,然后对数列中的每个数加上x的若干倍数(其中的x对于每个数而言均相同),使得数列中的所有数均相等2.由导论1可以推出,x一定是\(|a[i]-a[j]|(1\leqi,j\leqn)\)的最大公约数3.导论2的时间复杂度显然太高了,因此猜想\(gcd(|a[i......
  • CF1902 C Insert and Equalize 题解
    LinkCF1902CInsertandEqualizeQuestion有一个\(n\)个元素的数组\(a\),每个元素都不一样现在我们需要在\(a\)中添加一个数字\(a_{n+1}\),和之前的元素都不一样然后选择一个数\(x\),可以在一个元素上加\(x\),为操作一次,(每次加的数都是\(x\))求,操作的最少次数Solution......
  • [Codeforces] CF1799B Equalize by Divide
    序列操作(divide.cpp)—CF1799B—1200题目描述给您一个\(a_1,a_2,\dotsa_n\)这样的正整数数组,您可以对它进行多次(可以是零次)这样的操作:选择两个索引\(i,j(1\leqi,j\leqn,i\neqj)\);将\(a_i\)赋值为\(\lceil\frac{a_i}{a_j}\rceil\)。这里的\(\lceilx\rceil\)......
  • CF1862G The Great Equalizer
    题目链接先不考虑修改操作。直接模拟题目意思,可以发现最后留下的一定是最小的数字(因为相同的数每次会保留第一个)。我当时是顺着这个思路做的题目,现在想想反过来想好像会让问题变得更简单,即认为每次保留最后一个相同的数字。那么现在每次留下的就是最后一个数字,显然每次操作会让......