Cod sursa(job #1665035)

Utilizator medicinedoctoralexandru medicinedoctor Data 26 martie 2016 15:29:22
Problema Elementul majoritar Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
using namespace std;

int n,p=0,a[1000000],q[1000000],e;

int main()
{
	ifstream fi("elmaj.in");
	fi >> n;
	for (int i=1; i<=n; i++)
		fi >> a[i];
	int x;
	for (int i=1; i<=n; i++)
	{
		if (p==0) { p=1; q[1]=a[i]; } else
		{
			x=1;
			for (int j=p; j>=1; j--)
				if (a[i]!=q[j]) { x=0; q[j]=0; p=p-1; break; }
			if (x==1) { p=p+1; q[p]=a[i]; }
		}
	}
	for (int i=1; i<=n; i++)
		if (q[i]!=0) { e=q[i]; break; }
	ofstream f("elmaj.out");
	int w=0;
	for (long i=1; i<=n; i++)
		if (a[i]==e) w++;
	if (e!=0) f << e << ' ' << w; else f << -1;
	return 0;
}