Cod sursa(job #2443255)

Utilizator gheorghe_cristiGheorghe Florin Cristi gheorghe_cristi Data 27 iulie 2019 11:06:45
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

int cb(long v[], int l, int x)
{
    int st=1,dr=l,mij;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(v[mij]<=x)
            st=mij+1;
        else
            dr=mij-1;
    }
    return dr;
}

long v[3002];

int main()
{
    int n,c=0;
    long long s=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-2;++i)
        for(int j=i+1;j<=n-1;++j)
        {
            s=v[i]+v[j];
            c+=cb(v,n,s)-j;
        }
    fout<<c;
}