Pagini recente » Cod sursa (job #341838) | Cod sursa (job #1786514) | Cod sursa (job #2425135) | Cod sursa (job #2681545) | Cod sursa (job #856614)
Cod sursa(job #856614)
#include <fstream>
#include <cstdlib>
#include <tr1/unordered_map>
using namespace std;
using namespace tr1;
ifstream in("elmaj.in");
ofstream out("elmaj.out");
int n;
int v[1000100];
int main(){
in>>n;
srand(getpid());
unordered_map<int,int> hash;
for(int i=1;i<=n;++i){
in>>v[i];
}
for(int i=1;i<=3000;++i){
int poz=rand()%n + 1;
hash[v[poz]]++;
}
unordered_map<int,int>::iterator it;
int elmaj;
int ap=0;
for(it=hash.begin();it!=hash.end();++it){
if(it->second>ap){
elmaj=it->first;
ap=it->second;
}
}
ap=0;
for(int i=1;i<=n;++i){
if(v[i]==elmaj){
ap++;
}
}
if(ap>=n/2){
out<<elmaj<<" "<<ap;
return 0;
}
out<<"-1";
return 0;
}