1773: Mode
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 119 Solved: 46
[Submit][Status][Web Board]
Description
Give you a number of n series, in which a number of more than n / 2 times , you should find out that the number (n<=500000,x<=10^19)
Input
The first line is a positive integer n. The second line of n positive integers separated by spaces.
Output
A Positive integer on a line indicates that number
Sample Input
5
3 2 3 1 3
Sample Output
3
HINT
Source
//由于x十分大,直接用数组肯定是不行的,所以要想到map来计数
Ac_code:
#include <stdio.h>
#include <map>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
map<long long ,int>a;
long long x,flag = 1;
for(int i = 0; i < n; i++)
{
scanf("%lld",&x);
if(flag)
a[x]++;
if(a[x] > n/2 && flag)
{
printf("%lld\n",x);
flag = 0;
}
}
}
return 0;
}