Cod sursa(job #2231883)

Utilizator _ruthhhhhCapris Ruth _ruthhhhh Data 16 august 2018 13:53:30
Problema Numarare triunghiuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <algorithm>

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

int n;
int v[802];

int cautbin(int x)
{
    int stanga = 1, dreapta = n, rasp = -1, mijloc;
    while(stanga <= dreapta)
    {
        mijloc = (stanga + dreapta) / 2;
        if(v[mijloc] <= x)
        {
            rasp = mijloc;
            stanga = mijloc + 1;
        }
        else
            dreapta = mijloc - 1;
    }
    return rasp;
}

int main()
{
    int nr = 0, k;
    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++)
        for(int j = i + 1; j <= n; j++)
        {
            k = cautbin(v[i] + v[j]);
            nr += k - j;
        }
        fout << nr;
        return 0;
}