Cod sursa(job #2398740)

Utilizator StasBrega Stanislav Stas Data 5 aprilie 2019 23:41:36
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.57 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax=805;

int N,a[NMax],lg,s;

int main()
{

    fin >> N;

    for(int i=1;i<=N;i++)
        fin >> a[i];

    sort(a+1,a+1+N);

    for(int i=1;i<N;i++)
        for(int j=i+1;j<=N;j++)
        {
            int poz;
            for(lg=1;lg<=N;lg<<=1);
            for(poz=1;lg;lg>>=1)
                if(poz+lg<=N and a[poz+lg]<=a[i]+a[j])
                    poz+=lg;
            s+=poz-j;
        }

    fout << s;

    return 0;

}