Cod sursa(job #657999)
| Utilizator | Data | 7 ianuarie 2012 18:59:30 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.41 kb |
#include <iostream>
#include <fstream>
#include <algorithm>
#define DN 1000005
using namespace std;
int n,v[DN];
int main()
{
ifstream f("elmaj.in");
ofstream g("elmaj.out");
f>>n;
for(int i=1; i<=n; ++i) f>>v[i];
int p=n/2;
nth_element(v+1,v+p+1,v+n+1);
int el=v[p],nr=0;
for(int i=1; i<=n; ++i) if(v[i]==el) ++nr;
if(nr>p) g<<el<<' '<<nr;
else g<<"-1";
return 0;
}
