Cod sursa(job #1770040)

Utilizator ionanghelinaIonut Anghelina ionanghelina Data 3 octombrie 2016 18:08:04
Problema Oite Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.97 kb
#include<bits/stdc++.h>
#define cheie 666013
#define maxC 1035
#define dim 1005
using namespace std;
typedef struct tip
{
  int sum,a,b;
};
char buff[dim+5];
vector<tip> h[cheie+5];
vector<tip>::iterator it;
void hadd(tip x)
{
    int y=x.sum%cheie;
    h[y].push_back(x);
}
long long hsearch(int x,int a,int b)
{
    int y=x%cheie,suma,v1,v2;
    long long sol=0;
    for(it=h[y].begin();it!=h[y].end();it++)
    {
        suma=(*it).sum;
        v1=(*it).a;
        v2=(*it).b;
        if(suma!=x) continue;
        if(v1==a) continue;
        if (v1==b) continue;
        if(v2==a) continue;
        if(v2==b) continue;
        sol=sol+1LL;
    }
    return sol;
}
int poz=0;
void citeste(int &numar)
{
    while(buff[poz]<'0' || buff[poz]>'9')
    {
        poz++;
        if(poz==dim)
        {
            fread(buff,1,dim,stdin);
            poz=0;
        }
    }
    while(buff[poz]>='0' && buff[poz]<='9')
    {
        numar=numar*10+buff[poz]-'0';
        poz++;
        if(poz==dim)
        {
            fread(buff,1,dim,stdin);
            poz=0;
        }
    }
}
int n,l,v[maxC];
long long posibilitati;
int main()
{
    freopen("oite.in","r",stdin);
    freopen("oite.out","w",stdout);
    fread(buff,1,dim,stdin);
    citeste(n);
    citeste(l);
    for(int i=1;i<=n;i++)
    {
        citeste(v[i]);
    }
    sort(v+1,v+n+1);
    for(int i=1;i<=n;i++)
    {
        for(int j=(i+1);j<=n && (v[i]+v[j])<=l;j++)
        {
            int sum=v[i]+v[j];
            tip x;
            x.sum=sum;
            x.a=i;
            x.b=j;
            hadd(x);
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=(i+1);j<=n && (v[i]+v[j])<=l;j++)
        {
            {
                int dif=l-v[i]-v[j];
                posibilitati=posibilitati+hsearch(dif,i,j);
            }
        }
    }
    posibilitati>>=1;
    posibilitati/=3LL;
    printf("%lld\n",posibilitati);
    return 0;
}