Cod sursa(job #2251046)
| Utilizator | Data | 1 octombrie 2018 09:02:02 | |
|---|---|---|---|
| Problema | Elementul majoritar | Scor | 50 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.69 kb |
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout ("elmaj.out");
int x, n, i, ap, maj;
vector<int> v;
int main()
{
fin >> n;
for(i=0; i<n; i++)
{
fin >> x;
v.push_back(x);
}
maj=v[0]; ap=1;
for(i=1; i<n; i++)
{
if(v[i] == maj)
ap++;
else if(v[i]!=maj)
ap--;
else if(ap < 0)
{
maj = v[i];
ap = 1;
}
}
int nr = 0;
for(i=0; i<n && nr<n/2+1; i++)
if(v[i] == maj)
nr++;
if(nr>=n/2+1)
fout << maj << ' ' << nr;
return 0;
}
