[PAT解题报告] The Dominant Color

众数问题,从m * n个数中找出出现多于一半的数——保证存在。
本质: 同时删掉两个不同的数,众数不变。 于是我们随便记录一个数x, 来一个数 y, 和x不同的话就把x ,y都扔了,相当于扔掉两个不同的数,和x相同的话,就把计数器加1。
所以操作简化为
(1) x出现次数的计数器加1
(2) x出现次数的计数器减1

#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

int main() {
int m,n,answer;
    scanf("%d%d",&m,&n);
    for (int i = m * n, time = 0; i; --i) {
        int x;
        scanf("%d",&x);
        if (time == 0) {
            answer = x;
        }
        if (x == answer) {
            ++time;
        }
        else {
            --time;
        }
    }
    printf("%d\n",answer);
    return 0;
}


原题链接: http://www.patest.cn/contests/pat-a-practise/1054
全部评论
这平台好耗内存啊pat上提交308k这里居然有7w多直接ole。。。。。。
点赞 回复 分享
发布于 2015-09-11 16:33

相关推荐

Aki-Tomoya:窝趣,人家这是先富带动后富,共同富裕了属于是
投递英伟达等公司9个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务