Cod sursa(job #1910609)

Utilizator greenadexIulia Harasim greenadex Data 7 martie 2017 17:39:27
Problema Oite Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

#define pb push_back
#define f first
#define s second
#define pii pair<int, int>
#define mp make_pair
 
using namespace std;
 
const string name = "oite",
             in_file = name + ".in",
             out_file = name + ".out";
 
ifstream fin(in_file);
ofstream fout(out_file);
 
const int MAX = (1 << 10) + 5;

unsigned n, sum;
unsigned v[MAX];
long long sol;
unordered_map<unsigned, int> sums_of_two;

int main() {
	fin >> n >> sum;
	
	for (unsigned i = 1; i <= n; i++) {
		fin >> v[i];
	}

	sort(v + 1, v + n + 1);
	sums_of_two[v[1] + v[2]]++;

	for (unsigned poz_third = 3; poz_third < n; poz_third++) {
		for (unsigned poz_forth = poz_third + 1; poz_forth <= n; poz_forth++) {
			unsigned rest_sum = sum - v[poz_third] - v[poz_forth];
			
			if (rest_sum < 0) {
				break;
			}

			if (sums_of_two.count(rest_sum)) {
				sol += sums_of_two[rest_sum];
			}
		}

		for (unsigned i = 1; i < poz_third; i++) {
			sums_of_two[v[i] + v[poz_third]]++;
		}
	}

	fout << sol;
	return 0;
}