Cod sursa(job #1083475)
Utilizator | Lilian Ciobanu lilian_ciobanu | Data | 15 ianuarie 2014 23:54:41 |
---|---|---|---|
Problema | Elementul majoritar | Scor | 90 |
Compilator | cpp | Status | done |
Runda | Teme Pregatire ACM Unibuc 2013 | Marime | 0.65 kb |
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
int main(){
int n,i,x;
map<int , int> a;
map<int ,int>::iterator it;
f>>n;
for(i=1; i<=n; ++i){
f>>x;
a.insert(pair<int ,int>(x,0));
a[x]++;
}
int m=n/2 + 1,r=-1;
for(it=a.begin(); it!=a.end(); ++it){
if(it->second >= m) {
r=it->first;
x=it->second;
break;
}
}
if(r!=-1)
g<<r<<" "<<x;
else
g<<r;
return 0;
}