Cod sursa(job #2651333)

Utilizator alexia208160Popescu Alexia Maria alexia208160 Data 22 septembrie 2020 11:50:03
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#include <algorithm>

using namespace std;

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

int v[801];

int cb( int n, int ls, int x)
{
    int ld = n - 1, mij;
    while (ls <= ld)
    {
        mij = (ls + ld) / 2;
        if (v[mij] == x) return mij;
        if (v[mij] < x) ls = mij + 1;
        else ld = mij - 1;
    }
    return ld;
}

int main(void)
{
    int n, ct = 0;
    fin >> n;
    for (int i = 0; i < n; i++)
        fin >> v[i];
    sort(v, v + n);
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            int s = v[i] + v[j];
            int poz = cb( n, j + 1, s);
            while (v[poz] == v[poz + 1] && poz + 1 < n)
                poz++;
            ct += poz - j;
        }
    }
    fout << ct;
    return 0;
}