信息学奥赛一本通 1186:出现次数超过一半的数

题目链接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1186

1.利用桶的思想

时间复杂度为 O ( max ⁡ ( n , m ) ) O(\max(n, m)) O(max(n,m)),空间复杂度为 O ( m ) O(m) O(m)

#include <iostream>

using namespace std;

const int N = 1010, M = 110;

int n, bucket[M];

int main()
{
    cin >> n;
    
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        bucket[x + 50]++;
    }
    
    for (int i = 0; i <= 100; i++)
    {
        if (bucket[i] > n / 2)
        {
            cout << i - 50 << endl;
            return 0;
        }
    }
    
    cout << "no" << endl;
    
    return 0;
}

2.摩尔投票法

摩尔投票算法(Boyer-Moore majority vote algorithm)的思想是对拼消耗,从第一位选手开始闯关,遇到“非同类”就“同归于尽”,遇到“同类”就相伴而行,最后剩下的选手即为所求。

如果数组中有一个数字出现的次数超过数组长度的一半,那么这个数字出现的次数比其他所有数字出现的次数的和还要多。

初始时 r e s = a [ 0 ] res=a[0] res=a[0] c n t = 0 cnt = 0 cnt=0,遍历数组的时候更新答案 r e s res res 和次数 c n t cnt cnt

  • 如果 a [ i ] a[i] a[i] 和之前保存的数字 r e s res res 相同,则次数 c n t cnt cnt 1 1 1
  • 如果 a [ i ] a[i] a[i] 和之前保存的数字 r e s res res 不相同,则次数 c n t cnt cnt 1 1 1
  • 如果次数为 0 0 0,则保存 a [ i ] a[i] a[i],并把次数 c n t cnt cnt 设为 1 1 1

由于要找的数字出现的次数比其他所有数字出现的次数之和还要多,则最后一次把次数设为 1 1 1 时对应的数字就是要找的数字。

注意:在任何数组中,出现次数超过总数一半的数一定最多只有一个。

如果数组中存在这样的数,那么摩尔投票法最后得到的结果即为所求。

如果数组中不存在这样的数,例如 [ 1 , 2 , 1 , 2 , 3 ] [1, 2, 1, 2, 3] [1,2,1,2,3],则要校验最后的 r e s res res 的出现次数是否超过了数组长度的一半,即要重新遍历数组、统计出现的次数。

时间复杂度为 O ( n ) O(n) O(n),空间复杂度为 O ( 1 ) O(1) O(1)

#include <iostream>

using namespace std;

const int N = 1010;

int n, a[N];

int main()
{
    cin >> n;
    
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    
    int res = a[0], cnt = 0;
    
    for (int i = 1; i < n; i++)
    {
        if (cnt == 0)
        {
            res = a[i];
            cnt = 1;
        }
        else
        {
            if (a[i] != res)
            {
                cnt--;
            }
            else
            {
                cnt++;
            }
        }
    }
    
    // 最后要校验res的出现次数是否超过了一半
    int t = 0;
    for (int i = 0; i < n; i++)
    {
        t += (a[i] == res);
    }
    
    if (t > n / 2)
    {
        cout << res << endl;
    }
    else
    {
        cout << "no" << endl;
    }
    
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息学奥赛一本通1255:迷宫问题是一个关于迷宫的问题。这个问题要求通过广搜算来解决迷宫问题,找到走出迷宫的路径。具体来说,迷宫可以看成是由n×n的格点组成,每个格点只有两种状态, "." 和 "#" 。其中 "." 代表可通行的路径,"#" 代表不可通行的墙壁。通过广搜算,我们可以搜索从起点到终点的路径,找到一条合的路径即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [信息学奥赛一本通 1255:迷宫问题 | OpenJudge NOI 2.5 7084:迷宫问题](https://blog.csdn.net/lq1990717/article/details/124721407)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [c++信息学奥赛一本通1215题解](https://download.csdn.net/download/Asad_Yuen/87357807)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [信息学奥赛一本通(1255:迷宫问题)](https://blog.csdn.net/lvcheng0309/article/details/118879231)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值