zoukankan      html  css  js  c++  java
  • [洛谷P2397]yyy loves Maths VI (mode)

    题目大意:有$n(nleqslant2 imes10^6)$个数,找众数,保证众数出现次数超过一半(内存必须是$O(1)$)

    题解:摩尔投票法。因为众数出现次数超过一半,所以这个众数必然会只有$1$个。

    每一轮投票时,从数组中找出一对不同的元素,将它们删除。如果数组为空,则此时没有任何元素出现的次数超过该数组长度的一半。若不是空的,则此时数组内剩下的数出现此时超过一半。

    所以可以用一个计数器存一下现在出现次数最多的数比现有的数的一半多多少,当计数器等于$0$时,更新答案,最后剩下的一定是出现次数大于一半的数

    卡点:

    C++ Code:

    #include <cstdio>
    #include <cctype>
    namespace R {
    	int x;
    	char ch;
    	inline int read() {
    		ch = getchar();
    		while (isspace(ch)) ch = getchar();
    		for (x = ch & 15, ch = getchar(); isdigit(ch); ch = getchar()) x = x * 10 + (ch & 15);
    		return x;
    	}
    }
    using R::read;
    
    int n, last, cnt;
    int main() {
    	n = read();
    	for (register int i = 1, x; i <= n; i++) {
    		x = read();
    		if (!cnt) last = x;
    		if (x != last) cnt--;
    		else cnt++;
    	}
    	printf("%d
    ", last);
    	return 0;
    }
    

      

  • 相关阅读:
    jenkins
    k8s 驱逐限制
    jenkins+k8s 实现持续集成
    镜像更新备份
    【工具分享】2020年4月 phpstorm2020.1 for mac
    【排坑】mac安装homebrew会遇到的各种问题解决方案
    记一次C盘扩容
    2018夏季工作台之再设计
    left join后面加上where条件浅析
    编程随想篇(2018夏)
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/9898102.html
Copyright © 2011-2022 走看看