Cod sursa(job #1206290)

Utilizator IonSebastianIon Sebastian IonSebastian Data 9 iulie 2014 14:13:50
Problema Oite Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;

ifstream in("oite.in");
ofstream out("oite.out");

const int MAX_C = 1024, KEY = 666013;

//int val[KEY], lst[MAX_C+1], urm[MAX_C+1], nr;

/*bool inline contains(int element)
{
    int p = lst[element%KEY];
    while(p != 0 && val[p] != element)
    {
        p = urm[p];
    }
    return p != 0;
}

void inline push(int element)
{
    int p = element%KEY;
    val[++nr] = element;
    urm[nr] = lst[p];
    lst[p] = nr;
}

void inline pop(int element)
{
    int p = element%KEY;
    if(element == val[lst[p]])
    {
        lst[p] = urm[lst[p]];
        return;
    }
    p = lst[p];
    while(val[urm[p]] != 0 && val[urm[p]] != element)
    {
        p = urm[p];
    }
    if(val[urm[p]] == element)
    {
        urm[p] = urm[urm[p]];
    }
}*/

int v[MAX_C+1];

vector<int> h[KEY];

int main()
{
    int c, l, i, j;
    int ap = 0, sol = 0;
    int x;
    in >> c >> l;

    for(i = 1; i <= c; i++)
    {
        in >> v[i];
    }
    sort(v+1, v+c+1);
    for(i = 1; i <= c; i++)
    {
        for(j = i+1; j <= c; j++)
        {
            x = l-v[i]-v[j];
            if(x >= 0)
            for(int k = 0; k < h[x%KEY].size(); k++)
            {
                if(h[x%KEY][k] == x)
                {
                    sol++;
                }
            }
            /*while(contains(x))
            {
                ap++;
                pop(x);
            }
            sol += ap;
            while(ap > 0)
            {
                push(x);
                ap--;
            }*/
        }
        for(j = i-1; j > 0; j--)
        {
            //push(v[i]+v[j]);
            h[(v[i]+v[j])%KEY].push_back(v[i]+v[j]);
        }
    }
    out << sol;
    return 0;
}