Cod sursa(job #2040253)
| Utilizator | Data | 15 octombrie 2017 16:04:31 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.76 kb |
#include <fstream>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int N,V[1000005],i,ap,val,E;
int elem();
bool verif();
int main()
{
E=elem();
if(verif())
fout<<E<<' '<<ap<<'\n';
else
fout<<"-1\n";
return 0;
}
int elem()
{
fin>>N;
fin>>V[1];
val=V[1];
ap=1;
for(i=2; i<=N; i++)
{
fin>>V[i];
if(V[i]==val)
ap++;
else
{
ap--;
if(ap==0)
{
ap=1;
val=V[i];
}
}
}
return val;
}
bool verif()
{ap=0;
for(i=1;i<=N;i++)
if(V[i]==val)
ap++;
if(ap>=N/2+1)
return 1;
return 0;
}
