Pagini recente » Cod sursa (job #989307) | Cod sursa (job #1856729) | Cod sursa (job #2544403) | Cod sursa (job #1973911) | Cod sursa (job #2390225)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("elmaj.in");
ofstream fout("elmaj.out");
int n, v[1000003];
int em, cnt;
int main()
{
int i, j;
fin >> n;
for( i = 1; i <= n; i++)
fin >> v[i];
em = v[1];
cnt = 1;
for (i = 2; i <= n; i++)
{
if (v[i] == em)
cnt++;
else
{
cnt--;
if(cnt < 0)
{
em = v[i];
cnt = 1;
}
}
}
cnt = 0;
for(i = 1; i <= n; i++)
if(v[i] == em)
cnt++;
if(cnt >= n/2 + 1)
fout << em << " " << cnt << "\n";
else
fout<<"-1\n";
return 0;
}