Cod sursa(job #2125688)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 8 februarie 2018 17:31:33
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

int v[803], n;

int caut(int value) {
    int l = 1, r = n, ans = 0;

    while (l <= r) {
        int m = (l + r) >> 1;
        if (v[m] <= value) {
            ans = m;
            l = m + 1;
        } else {
            r = m - 1;
        }
    }

    return ans;
}

int main()
{
    int ct = 0;

    fin >> n;

    for(int i = 1; i <= n; i++)
        fin >> v[i];

    sort(v + 1, v + n + 1);

    for(int i = 1; i < n - 1; i++) {
        int k = v[i] + v[i + 1];

        int x = caut(k);

        if(x > i + 1)
           ct += x - i - 1;
    }

    fout << ct;

    return 0;
}