Cod sursa(job #3233333)

Utilizator MirceaDonciuLicentaLicenta Mircea Donciu MirceaDonciuLicenta Data 2 iunie 2024 23:38:08
Problema Oite Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>

using namespace std;

int main() {
    ifstream infile("oite.in");
    ofstream outfile("oite.out");

    int C;
    long long L;
    infile >> C >> L;

    vector<long long> A(C);
    for (int i = 0; i < C; ++i) {
        infile >> A[i];
    }

    int count = 0;

    // Use a hash map to store pairs of sums
    unordered_map<long long, int> pairs_sum;

    // Iterate over all pairs of elements
    for (int i = 0; i < C; ++i) {
        for (int j = i + 1; j < C; ++j) {
            long long current_sum = A[i] + A[j];

            // Check if there exists another pair that together with the current pair sums to L
            if (pairs_sum.find(L - current_sum) != pairs_sum.end()) {
                count += pairs_sum[L - current_sum];
            }
        }

        // After processing pairs with the i-th element, store their sums in the hash map
        for (int k = 0; k < i; ++k) {
            pairs_sum[A[i] + A[k]]++;
        }
    }

    outfile << count << endl;

    infile.close();
    outfile.close();

    return 0;
}