Cod sursa(job #2544217)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 11 februarie 2020 20:51:40
Problema Medie Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("medie.in");
ofstream fout("medie.out");

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

int main()
{
    fin >> n;
    for (int i = 1; i <= n; ++i)
    {
        fin >> v[i];
        mx = max(mx, v[i]);
        fr[v[i]]++;
    }
    for (int i = 1; i <= mx; ++i)
    {
        if (fr[i] == 0) continue;
        for (int j = 1; j <= n; ++j)
        {
            if (v[j] == i) continue;
            int x = 2 * i - v[j];
            if (x <= 0) continue;
            if ((v[j] + x) / 2.f != (float)i) continue;
            ans += 1LL * fr2[x] * fr[i];
            fr2[v[j]]++;
        }
        for (int j = 1; j <= mx; ++j) fr2[j] = 0;
    }
    for (int i = 1; i <= mx; ++i)
    {
        if (fr[i] < 3) continue;
        ans += 1LL * ((fr[i] * (fr[i] - 1)) / 2) * (fr[i] - 2);
    }
    fout << ans;
    fin.close();
    fout.close();
    return 0;
}