Cod sursa(job #2497669)
| Utilizator | Data | 23 noiembrie 2019 09:54:34 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.71 kb |
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int v[1000001];
int main()
{
ifstream fin ("elmaj.in");
ofstream fout ("elmaj.out");
int n, i, j, k, nr, cand;
fin>>n;
k=0;
for(i=1; i<=n; i++)
{
fin>>v[i];
if(k==0)
{
cand=v[i];
k=1;
}
else if(v[i]==cand)
{
k++;
}
else
{
k--;
}
}
nr=0;
for(i=1; i<=n; i++)
{
if(v[i]==cand)
{
nr++;
}
}
cout<<"nr= "<<nr<<"\n";
if(nr>=n/2+1) fout<<cand<<' '<<nr;
else fout<<-1;
}
