Cod sursa(job #709941)
Utilizator | Data | 8 martie 2012 18:39:20 | |
---|---|---|---|
Problema | Elementul majoritar | Scor | 50 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.44 kb |
#include <fstream>
#include <algorithm>
using namespace std;
int v[1000001];
int main()
{
ifstream f("elmaj.in");
ofstream g("elmaj.out");
bool ok=false;
int n, j, i, contor=1, contor2=0;
f>>n;
for(i=1; i<=n; i++)
{
f>>v[i];
}
sort(v+1, v+n+1);
for(i=n; i>1; i--)
{
if(v[i]==v[i-1])
contor++;
else
contor=1;
if(contor>n/2)
{
g<<v[i]<<" "<<contor;
ok=true;
}
}
if(!ok)
g<<-1;
}