Cod sursa(job #1444263)

Utilizator LegionHagiu Stefan Legion Data 29 mai 2015 14:50:15
Problema Medie Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int a[9005];
int main()
{
	ifstream in("medie.in");
	ofstream out("medie.out");
	int i, n, j, x, y, mij, tot = 0,z;
	in >> n;
	for (i = 1; i <= n; i++)
	{
		in >> a[i];
	}
	sort(a + 1, a + n + 1);
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= n; j++)
		{
			if (j == i){ continue; }
			x = j+1;
			y = n;
			while (x <= y)
			{
				mij = (x + y) / 2;
				if (a[mij] + a[j] == 2*a[i])
				{
					z = mij-1;
					while (a[mij] == a[z]&&z>j)
					{
						tot++;
						if (z == j || z == i){ tot--; }
						z--;
					}
					z = mij + 1;
					while (a[mij] == a[z])
					{
						tot++;
						if (z == j || z == i){ tot--; }
						z++;
					}
					if (mij!=j&&mij!=i)
					tot++;
					x = y + 1;
				}
				else if ((a[mij] + a[j]) < 2*a[i])
				{
					x = mij + 1;
				}
				else
				{
					y = mij - 1;
				}
			}
		}
	}
	out << tot;
}