首页 > 试题广场 >

小欧的数组修改

[编程题]小欧的数组修改
  • 热度指数:1329 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
小欧拿到了一个数组,她可以修改其中任意一个元素的值(也可以不修改),使得出现次数最多的那个元素次数尽可能多。你能求出这个最多的出现次数吗?

输入描述:
第一行输入一个正整数n,代表数组的大小。
第二行输入n个正整数a_i,代表数组的各个元素。


输出描述:
一个正整数,代表小欧操作后出现最多的元素次数。
示例1

输入

3
1 2 3

输出

2

说明

将1修改为3,数组变成[3,2,3],3出现了2次。修改方式并不是唯一的。
示例2

输入

1
4

输出

1

说明

由于只有一个数,所以无论是否进行修改,它都只出现了1次。
头像 kkkyd
发表于 2024-11-20 20:06:22
#include<bits/stdc++.h> using namespace std; int main() { unordered_map<int,int>p;//记录出现次数 int n,m,max_num=0; cin>>n; 展开全文
头像 Mag1c0nch
发表于 2024-11-21 14:10:29
考虑使用map维护每个数出现的次数,发现可以顺便维护下map中最大的值 maxn ,答案就是 min(maxn + 1, n) #include <bits/stdc++.h> using namespace std; #define int long long const int N 展开全文
头像 Kato_Shoko
发表于 2024-11-19 23:27:57
map老朋友了。 #include <iostream> #include <queue> #include <map> #include <set> #include <cmath> #include <cstring> #i 展开全文
头像 宿伞之神
发表于 2024-11-20 00:13:48
计数。 #include<bits/stdc++.h> #define int long long #define double long double #define x first #define y second using namespace std; typedef long 展开全文
头像 tartarns_yan
发表于 2024-11-20 09:24:24
#include <bits/stdc++.h> using namespace std; map<int, int>times; int main() { int n, maxx = 0; cin >> n; for(int i = 0; 展开全文
头像 Silencer76
发表于 2025-01-08 16:05:08
解题思路 这道题目可以使用遍历+哈希表的方法解决。对于每个数组元素,我们可以: 将其作为可能被修改成的目标值 统计原数组中与该值相同的元素个数 由于可以修改一个其他的数,所以最终结果是该值的出现次数+1(如果还有其他不同的数) 代码 c++ java python #inclu 展开全文
头像 来泡池子了的西红柿很奔放
发表于 2024-11-26 13:35:39
#include <stdio.h> #include <stdlib.h> int main() { int n; if (scanf("%d", &n) != EOF) { int* a = (int*)mall 展开全文
头像 ctfer爱ak
发表于 2024-11-21 20:07:23
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll a[100010],b[100010]; int main() { int n,flag=1; cin>>n; fo 展开全文
头像 扎男_
发表于 2025-05-12 20:23:57
//活动地址: 牛客春招刷题训练营 - 编程打卡活动 #pragma clang diagnostic push #pragma ide diagnostic ignored "cppcoreguidelines-narrowing-conversions" #pragma 展开全文
头像 叫啥名
发表于 2025-05-15 14:40:36
// #牛客春招刷题训练营# https://www.nowcoder.com/discuss/726480854079250432 #include <iostream> #include <map> using namespace std; map<int,int 展开全文