Cod sursa(job #1900599)
| Utilizator | Data | 3 martie 2017 15:15:15 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.57 kb |
#include <fstream>
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
long long n,i,a[1000002],c,x,ct;
int main()
{
f >> n;
for (i=1;i<=n;i++)
f >> a[i];
c=0;
for (i=1;i<=n;i++)
{
if(c==0)
x=a[i];
if(a[i]==x)
{
c++;
}
else
if(a[i]!=x)
{
c--;
}
}
ct=0;
for (i=1;i<=n;i++)
if(a[i]==x) ct++;
if(c>n/2+1)
g << x << " " << ct;
else
g <<-1;
return 0;
}
