首页 > 其他分享 >**CodeForces CF1928B Equalize题解**

**CodeForces CF1928B Equalize题解**

时间:2024-07-08 09:10:07浏览次数:19  
标签:int 题解 CodeForces number CF1928B 序列 permutation test array

ok兄弟们,今天本蒟蒻来做一篇小小的题解

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 $ .

本题需要我们做的就是,在题目给的A数组的基础上,对其添加一个序列。

(序列中每个数字只能添加一次)

本题的关键就是要理解如何添加序列以及序列的关键节点。

那么该如何添加序列呢?首先,请各位思考一下,1和1可能在添加该序列后出现相同的值吗?因为同一个值是只能使用一次的,所以不能,不合法。因此,我们需要对原数组进行降重处理。

此外,如果原数组为{1,2,3,4,5,6,7}恰好为公差为1的等差数列,那么获取相等最多的数组就是将序列1~7反向添加,也就是{8,8,8,8,8,8,8}。那么我们去掉部分并延展这个原数列{1,4,7,12,13,14,15,16,20}

极端情况下思考,为了使相等的元素尽量多,我们会让原数组最小的值最大化,最大的值最小化,即最小的值加上序列的最大值,最大的值加上序列的最小值。如果满足a(min)+n>=a(max)+1,那么在原数组区间内(min~max),是不是有多少值,我们都有相对应的序列进行匹配,又因为我们进行了降重操作,所以无需考虑重复,因此,我们需要进行排序操作

思路综上所述,以下是代码实现

include<bits/stdc++.h>

using namespace std;
int t, n;
vectora(n);
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
vectora(n);
for (int i = 0; i < n ; i++) {
scanf("%d ", &a[i]);
}
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;

}

标签:int,题解,CodeForces,number,CF1928B,序列,permutation,test,array
From: https://www.cnblogs.com/lgdxxs12138/p/18289263

相关文章

  • codeforces1849 D. Array Painting
    题目链接https://codeforces.com/problemset/problem/1849/D题意输入\(n(1\leqn\leq2e5)\)和长为\(n\)的数组\(a(0\leqa[i]\leq2)\)。最初,数组的每个元素都是蓝色的。有两种类型的操作:支付一枚硬币,选择一个蓝色元素,将其涂成红色。选择一个不等于\(0\)的红......
  • qoj8225 最小值之和 题解
    题目链接点击打开链接题目解法很牛的题啊从\(f\)序列的最小值切入,考虑把\(f_i:=f_i-f_{min}\),会对\(f'\)造成什么影响?发现会使\(f'\)中的每个数都减去\((n-1)f_{min}\),且会把原问题分成\([1,min]\)和\([min+1,r]\)这两个完全相同的子问题于是考虑区间\(dp\),令......
  • 电脑开机检测不到硬盘怎么办 电脑检测不到硬盘问题解决
    电脑开机检测不到硬盘,无法进入系统或者显示“RebootandSelectproperBootdevice”等错误信息。这种情况可能会导致我们的数据丢失或者无法使用电脑。一、电脑检测不到硬盘的可能原因电脑检测不到硬盘的原因主要有以下几种:1、硬盘连接线松动或损坏:硬盘是通过SATA线或M.2插......
  • 启动应用程序出现wevtutil.exe找不到问题解决
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个wevtutil.exe文件(挑选合适的版本文件)把它......
  • 启动应用程序出现wininit.exe找不到问题解决
    其实很多用户玩单机游戏或者安装软件的时候就出现过这种问题,如果是新手第一时间会认为是软件或游戏出错了,其实并不是这样,其主要原因就是你电脑系统的该dll文件丢失了或没有安装一些系统软件平台所需要的动态链接库,这时你可以下载这个wininit.exe文件(挑选合适的版本文件)把它放......
  • Educational Codeforces Round 167 (Rated for Div. 2)
    A容易发现由于玩家是八向移动,-1以及其上的硬币都可以接到,但是往下都无法。B子序列不需要连续,子串则必须连续,那么我们可以考虑对子串进行遍历,相当于遍历起点,求出子序列能和其对上的最大长度,然后用子串长度加上子序列的长度减去重合长度即可。C赛时C没D出的快,想贪心策略想......
  • [SNCPC2024] 2024 年陕西省大学生程序设计 J题猜质数II 题解
    题目链接:CF或者洛谷PS:CF的得等上gym。前提说明其实在上个月就见到这题了,当时很想做这题,结果找不到做题链接,也不知道出处,原来是陕西省赛的捧杯题。个人评价觉得是一道很不错的题,难度适中。讲解其实题解写的挺不错的,比很多比赛的题解写的详细许多了。这里站在我的角度分......
  • [题解]细胞自动机
    给定一个长度为\(n\)的\(01\)串\(s\),用于表示一个环上的细胞的初始状态,其中第\(1\)个细胞与第\(2\)个、第\(n\)个细胞相邻;第\(n\)个细胞与第\(1\)个和第\(n-1\)个相邻。\(0\)表示细胞死亡,\(1\)表示细胞存活。接下来给定\(t\)轮操作,每一轮操作,根据上一轮细胞的状态更改此轮的状态。......
  • P9668 [ICPC2022 Jinan R] Torch 题解
    思路考虑使用矩阵模拟这个过程。首先,我们可以设初值为:\[\begin{bmatrix}0&1\end{bmatrix}\]表示瘦子初始走\(0\)米,胖子初始走\(1\)米。考虑瘦子走一步。由于瘦子每走一步都不能超过胖子,我们可以使用\((\min,+)\)矩乘来维护这个性质。那么瘦子走一步是:\[\begin{bma......
  • CF292C Beautiful IP Addresses 题解(两种写法)
    题意一个IP地址是一个32位的2进制整数,分成四组8位的2进制整数(没有前导0)。比如说,0.255.1.123 是一个正确的IP地址,而0.256.1.123 和 0.255.1.01 不是正确的。定义一个合法的回文IP地址为BeautifulIPAddress(回文地址就是去掉“.”后是个回文字符串的地......