[BZOJ2456] mode(一道很有意思的题)

传送门

 

看到这个题的第一反应是离散化+线段树乱搞。。

eeeeeeeeeeee感觉数据结构学傻了,其实直接存下来,sort一遍,n/2的位置的就是答案

当然前提是空间够的话

 

1m的空间连数组都开不下

于是有了一个很巧妙的思路,吼吧,直接给链接

http://blog.csdn.net/suncongbo/article/details/76862425

 

#include <cstdio>

int n, top, num, now;

int main()
{
	scanf("%d", &n);
	while(n--)
	{
		scanf("%d", &now);
		if(!num)
		{
			num++;
			top = now;
		}
		else if(now == top) num++;
		else num--;
	}
	printf("%d\n", top);
	return 0;
}

  

posted @ 2018-01-17 21:26  zht467  阅读(145)  评论(0编辑  收藏  举报