Cod sursa(job #2078333)

Utilizator AndreiJJIordan Andrei AndreiJJ Data 29 noiembrie 2017 12:44:42
Problema Numarare triunghiuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>
using namespace std;
int a[805], n;
int CautBinDr(int x, int p)
{
    int st, dr, mij, poz;
    st=p;
    dr=n;
    poz=0;
    while (st<=dr)
    {
        mij=(st+dr)/2;
        if (a[mij]<=x)
        {
            poz=mij;
            st=mij+1;
        }
        else dr=mij-1;
    }
    return poz;
}
int main()
{
    int i, j, x, cnt=0, poz;
    ifstream fin ("nrtri.in");
    ofstream fout ("nrtri.out");
    fin >> n;
    for (i=1; i<=n; i++)
        fin >> a[i];
    sort (a+1, a+n+1);
    for (i=1; i<=n-2; i++)
        for (j=i+1; j<=n-1; j++)
        {
            x=a[i]+a[j];
            poz=CautBinDr(x, j+1);
            if(poz!=0) cnt=cnt+(poz-j);
        }
    fout << cnt;
    return 0;
}