Cod sursa(job #2656444)

Utilizator hhhhhhhAndrei Boaca hhhhhhh Data 7 octombrie 2020 19:00:41
Problema Medie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
using namespace std;
using namespace __gnu_pbds;
auto random_address = [] { char *p = new char; delete p; return uint64_t(p); };
const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1);
mt19937_64 rng(SEED);
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ifstream fin("medie.in");
ofstream fout("medie.out");
ll n;
ll f[7001];
ll ans=0;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        int x;
        fin>>x;
        f[x]++;
    }
    for(int val=1;val<=7000;val++)
        if(f[val])
        {
            ll nrmoduri=f[val];
            f[val]--;
            ll st=val;
            ll dr=val;
            while(st>0&&dr<=7000)
            {
                if(st==dr)
                    ans+=(f[st]*(f[st]-1))/2*nrmoduri;
                else
                    ans+=f[st]*f[dr]*nrmoduri;
                st--;
                dr++;
            }
            f[val]++;
        }
    fout<<ans;
    return 0;
}