Cod sursa(job #1007891)
| Utilizator | Data | 9 octombrie 2013 20:53:18 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 90 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <fstream>
#include <map>
#define dim 1000001
using namespace std;
int n,k,a[dim];
int main()
{
ifstream cin("elmaj.in");
ofstream cout("elmaj.out");
map<int,int> h;
cin>>n;
k = n/2 + 1;
for (int i=0;i<n;i++)
{
cin>>a[i];
//retrieve hash
if (h[a[i]])
{
h[a[i]]++;
}
else
{
h[a[i]] = 1;
}
}
for (int i=0;i<n;i++)
{
if (h[a[i]] >= k)
{
cout<<a[i]<<" "<<h[a[i]]<<endl;
h[a[i]] = 0; //remove it
}
}
return 0;
}
