Cod sursa(job #2543899)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 11 februarie 2020 17:04:46
Problema Medie Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <bits/stdc++.h>

using namespace std;

ofstream fout("medie.out");

int n, v[9005], fr[7005];
long long ans;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};


int main()
{
    InParser fin("medie.in");
    fin >> n;
    for (int i = 1; i <= n; ++i)
    {
        fin >> v[i];
        ++fr[v[i]];
    }
    for (int i = 1; i < n; ++i)
    {
        for (int j = i + 1; j <= n; ++j)
        {
            if (v[i] % 2 != v[j] % 2) continue;
            fr[v[i]]--;
            fr[v[j]]--;
            int x = (v[i] + v[j]) / 2;
            ans += fr[x];
            fr[v[i]]++;
            fr[v[j]]++;
        }
    }
    fout << ans;
    fout.close();
    return 0;
}