首页 > 其他分享 >ZOJ 2132 the most frequent number

ZOJ 2132 the most frequent number

时间:2022-11-09 19:08:40浏览次数:41  
标签:2132 int ZOJ number most Input problem frequent


Description



Seven (actually six) problems may be somewhat few for a contest. But I am really unable to devise another problem related to Fantasy Game Series. So I make up an very easy problem as the closing problem for this contest.

Given a sequence of numbers A, for a number X if it has the most instances (elements of the same value as X) in A, then X is called one of the most frequent numbers of A. Now a sequence of numbers A of length L is given, and it is assumed that there is a number X which has more than L / 2 instances in A. Apparently X is the only one most frequent number of A. Could you find out X with a very limited memory?


Input



Input contains multiple test cases. Each test case there is one line, which starts with a number L (1 <= L <= 250000), followed by L numbers (-2^31 ~ 2^31-1). Adjacent numbers is separated by a blank space.


Output



There is one line for each test case, which is the only one most frequent number X.


Sample Input




5 2 1 2 3 2 8 3 3 4 4 4 4 3 4



Sample Output




2

4

找出数组中超过半数的那个数,限制了内存大小,可以用map或链表过,但其实有更简单的办法。

#include<iostream>  
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
int n, x, a, b;

int main()
{
while (~scanf("%d", &n))
{
for (int i = a = b = 0; i < n; i++)
{
scanf("%d", &x);
if (b == 0) { a = x; b = 1; }
else if (a == x) b++; else b--;
}
printf("%d\n", a);
}
return 0;
}

标签:2132,int,ZOJ,number,most,Input,problem,frequent
From: https://blog.51cto.com/u_15870896/5838276

相关文章

  • ZOJ 3605 Find the Marble
    DescriptionAliceandBobareplayingagame.Thisgameisplayedwithseveralidenticalpotsandonemarble.Whenthegamestarts,Aliceputsthepotsinone......
  • ZOJ 2068 Chopsticks
    DescriptionIt'sDecember2nd,Mr.L'sbirthday!HeinvitedKpeopletojoinhisbirthdayparty,andwouldliketointroducehiswayofusingchopsticks.So,he......
  • ZOJ 3911 Prime Query
    PrimeQueryTimeLimit: 1Second     MemoryLimit: 196608KBYouaregivenasimpletask.Givenasequence A[i] with NHerearetheoperations:Av......
  • ZOJ 3905 Cake
    CakeTimeLimit: 4Seconds     MemoryLimit: 65536KBAliceandBoblikeeatingcakeverymuch.Oneday,AliceandBobwenttoabakeryandboughtm......
  • [bzoj3033] 太鼓达人 (欧拉回路)
    学会了欧拉回路pwpwpwpwpwpDescription七夕祭上,Vani牵着cl的手,在明亮的灯光和欢乐的气氛中愉快地穿行。这时,在前面忽然出现了一台太鼓达人机台,而在机台前坐着的是......
  • 2022-2023-1 20221328《计算机基础与程序设计》第十周学习总结
    作业信息班级:首页-2022-2023-1-计算机基础与程序设计-北京电子科技学院-班级博客-博客园(cnblogs.com)作业要求:2022-2023-1《计算机基础与程序设计》教学进程......
  • 2022-2023-1 20221325《计算机基础与程序设计》第十周学习总结
    作业信息这个作业属于那个班级:https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP作业要求:https://www.cnblogs.com/rocedu/p/9577842.html#WEEK10作业目标:学习......
  • 2022-2023-1 20221322《计算机基础与程序设计》第十周学习总结
    作业信息这个作业属于哪个课程<班级的链接>(2022-2023-1-计算机基础与程序设计)这个作业要求在哪里<作业要求的链接>(2022-2023-1计算机基础与程序设计第十周作业......
  • BZOJ P3732 Network(Kruskal重构树)
    Network题目描述:给\(N\)个点的无向图\(\left(1\leqN\leq15000\right)\),记为:\(1\dotsN\)图中有\(M\)条边\(\left(1\leqM\leq30000\right)\),第\(i\)......
  • 11. Container With Most Water
    Given n non-negativeintegers a1, a2,..., an,whereeachrepresentsapointatcoordinate(i, ai). n verticallinesaredrawnsuchthatthetwoendpoi......