Cod sursa(job #1905492)

Utilizator raulmuresanRaul Muresan raulmuresan Data 6 martie 2017 08:38:12
Problema Oite Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;

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

unsigned int sum;
unsigned int n, i ,j;
vector<unsigned int> elements;


int main()
{
    fin>> n >> sum;
    for(i = 0; i < n; i++)
    {
        int x;
        fin >> x;
        elements.push_back(x);
    }
    sort(elements.begin(), elements.end());
    for(i = 0; i < n; i++)
    {
        //fout << elements[i] << " ";
    }

    unordered_map<unsigned int , unsigned int> hash;
    unsigned int result = 0;

    for(i = 0; i < n - 1 && elements[i] <= sum; ++i )
    {
        for(j = i + 1; j < n && elements[i] + elements[j] <= sum; ++j )
            result += hash[sum - elements[i] - elements[j]];

        //adaugam noile perechi in hash
        for(j = 0; j < i; ++j )
            hash[elements[i] + elements[j]]++;
    }

    fout << result << "\n";


    return 0;
}