题解 | B. 找数

找数

https://ac.nowcoder.com/acm/contest/121614/B

B. 找数

题意让我们找到出现次数最多的,数值最小的数,这是一个双关键字视角下求最值的问题

注意到值域取 ,我们可以用 存储 的出现次数,再扫一遍即可

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define fType int
#define foreach(x, a, b) for (fType x = a, _end_ = b; x <= _end_; x++)
#define foreach_sub(x, a, b) for (fType x = a, _end_ = b; x >= _end_; x--)
#define tpl make_tuple

constexpr int N = 100000;
int n, x, cnt[N + 1];

int main()
{
    ios::sync_with_stdio(0), cin.tie(0);

    cin >> n;
    foreach (i, 1, n)
    {
        cin >> x;
        ++cnt[x];
    }

    using pr = pair<int, int>;
    pr ans = {0, 0};
    foreach (x, 0, N)
        ans = max(ans, pr(cnt[x], -x));
    cout << -ans.second << "\n";

    return 0;
}

扩展思考:如果值域是 ,而 怎么做呢?

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务