Cod sursa(job #1652505)

Utilizator medicinedoctoralexandru medicinedoctor Data 14 martie 2016 23:39:42
Problema Elementul majoritar Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
using namespace std;

float a[1000000],q;
long long n;

void lire()
{
	long long i;
	ifstream f("elmaj.in");
	f >> n;
	for (i=1; i<=n; i++)
		f >> a[i];
	f.close();
}

void sw(float *x,float *y)
{
	float q=*x;
	*x=*y;
	*y=q;
}

void qs(long long l, long long r)
{
	long long i=l,j=r; float q=a[(i+j)/2];
	while (i<j)
	{
		while (a[i]<q) i++;
		while (a[j]>q) j--;
		if (i<=j)
		{
			sw(&a[i],&a[j]);
			i++;
			j--;
		}
	}
	if (i<r) qs(i,r);
	if (j>l) qs(l,j);
}

long long lu()
{
	long long i,x=1,y=0; float r=a[1];
	for (i=2; i<=n; i++)
		if (a[i]==r) x=x+1; else { if (x>y) { y=x; q=r; } x=1; r=a[i]; }
	return y;
}

void ecrire(long long x)
{
	ofstream f("elmaj.out");
	if (x>(n / 2)) f << q << ' ' << x; else f << -1;
	f.close();
}

int main()
{
	lire();
	qs(1,n);
	ecrire(lu());
	return 0;
}