Cod sursa(job #244809)

Utilizator mika17Mihai Alex Ionescu mika17 Data 16 ianuarie 2009 00:12:28
Problema Oite Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#define fi first
#define se second
using namespace std;

const int NMAX = 1024, HSIZE = 3033;
const double knt = 0.61803398;
const pair <int,int> DUTE_TARE = make_pair(-1,-1);

int N,S,c[NMAX];
vector< pair<int,int> > hash[HSIZE],hash2[HSIZE];

inline int h(int x) {

        return int( (double(x * knt) - int(x * knt)) * (HSIZE-1) );
}

int search(int x)
{
        int hh = h(x);

        for(int i = hash[hh].size() - 1; i >= 0; --i)
          if(hash[hh][i] . fi == x) return i;

        return -1;
}

void insert(int x) {

        int hh = h(x), t = search(x);
        if(t == -1)
           hash[hh].push_back(make_pair(x,1));

         else hash[hh][t] . se ++;
}

void erase(int x) {

        int hh = h(x);

        for(int i = 0 ; i < (int)hash[hh].size(); ++i)
         if(hash[hh][i] . fi == x)
         {
          if(hash[hh][i] . se == 1)
            hash[hh][i] = DUTE_TARE;
           else hash[hh][i] . se --;
          return;
         }
}

int count(int x) {

        if(x<0) return 0;
        
        int hh = h(x);

        for(int i = hash[hh].size() - 1; i >= 0; --i)
        {
         if(hash[hh][i] . fi == x) return hash[hh][i] . se;
        }

        return 0;
}

void debug()
{
 for(int i=0;i < HSIZE; ++i)
 {
  for(int j=0; j < (int)hash[i].size(); ++j)
   printf("(%d %d) ",hash[i][j]. fi, hash[i][j] . se);
  printf("\n");
 }
}

int main() {

        freopen("oite.in","r",stdin);
        freopen("oite.out","w",stdout);

        scanf("%d%d",&N,&S);
        for(int i=1;i<=N;++i)
         scanf("%d",&c[i]);

        sort(c,c+N);

        for(int i=3;i<N;++i)
        {
         for(int j=i+1;j<=N;++j)
          insert(c[i]+c[j]);
        }

        //debug();

        int cnt = 0;
        for(int j=2;j<=N-2;++j)
        {
         for(int i=1; i<j; ++i)
          cnt += count(S - c[i] - c[j]);

         for(int i=j+2;i<=N;++i)
          erase(c[j+1] + c[i]);
        }

        printf("%d",cnt);

        return 0;
}