Cod sursa(job #918401)
Utilizator | Data | 18 martie 2013 20:43:18 | |
---|---|---|---|
Problema | Elementul majoritar | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.48 kb |
#include <fstream>
#include <algorithm>
using namespace std;
int v[1000010];
int main()
{
ifstream fi("elmaj.in");
ofstream fo("elmaj.out");
int n,i,elmaj,lo,hi,mid;
fi>>n;
mid=n/2+1;
for(int i=1;i<=n;++i)
fi>>v[i];
sort(v+1,v+n+1);
elmaj=v[mid];
lo=lower_bound(v+1,v+n+1,elmaj)-v;
hi=upper_bound(v+1,v+n+1,elmaj)-v-1;
if(hi-lo+1>=mid)
fo<<elmaj<<" "<<hi-lo+1;
else
fo<<"-1";
return 0;
}