Cod sursa(job #1242271)
| Utilizator | Data | 14 octombrie 2014 10:41:40 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <iostream>
#include <fstream>
using namespace std;
int a[1000005];
int main()
{
ifstream f("elmaj.in");
ofstream g("elmaj.out");
int n,ok=0,mx=0,x;
f >> n;
for(int i=1;i<=n;++i)
{
f >> a[i];
if(a[i]==mx)
{
++ok;
}
else --ok;
if(ok<0)
{
ok=1;
mx=a[i];
}
}
ok=0;
for(int i=1;i<=n;++i)
{
if(a[i]==mx)
{
++ok;
}
}
if(ok>=(n/2)+1) g << mx <<" " << ok;
else g << "-1";
return 0;
}
