Pagini recente » Cod sursa (job #2299722) | Cod sursa (job #324140) | Cod sursa (job #1495955) | Cod sursa (job #58684) | Cod sursa (job #1995832)
#include <fstream>
#define NMAX 1000000
using namespace std;
ifstream f("elmaj.in");
ofstream g("elmaj.out");
int v[NMAX+10],elmaj,cnt,i,n;
void _elmaj()
{
elmaj = v[1];
cnt = 0;
for( i = 1; i <= n; i ++ )
{
if( elmaj == v[i] )
cnt++;
else
cnt--;
if ( cnt < 0 )
{
elmaj = v[i];
cnt = 1;
}
}
cnt = 0;
for ( i = 1; i <= n; i ++)
if ( v[i] == elmaj )
cnt++;
if( cnt >= n / 2 + 1 )
g << elmaj << " " << cnt;
else
g << "-1";
}
int main()
{
f >> n;
for (i = 1; i<=n; i++)
f >> v[i];
_elmaj();
return 0;
}