Cod sursa(job #2657208)

Utilizator PletoPletosu Cosmin-Andrei Pleto Data 10 octombrie 2020 08:07:28
Problema Numarare triunghiuri Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
#include <algorithm>
#define NMAX 1001

using namespace std;

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

int N, sol, V[NMAX];

int search(int x) {
    int low = 0, high = N + 1, mid;

    while (high - low > 1) {
        mid = (low + high) / 2;
        if (V[mid] < x) {
            low = mid;
        } else {
            high = mid;
        }
    }
    
    if (high == N + 1 || V[high] != x) {
        return low;
    } else {
        return high;
    }
}

int main() {
    fin >> N;
    for (int i = 1; i <= N; ++i) {
        fin >> V[i];
    }

    sort(V + 1, V + N + 1);
    for (int i = 1; i <= N; ++i) {
        for(int j = i + 1; j <= N; ++j) {
            sol += search(V[i] + V[j]) - j;
        }
    }
    
    fout << sol << '\n';
}