Cod sursa(job #2429848)

Utilizator PredescuSebastianIonPredescu Sebastian Ion PredescuSebastianIon Data 11 iunie 2019 15:24:20
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>

using namespace std;
ifstream f("nrtri.in");
ofstream g("nrtri.out");
int n,a[802],st,dr,j,sol,mij;
int main()
{
    f>>n;
    for(int i=1; i<=n; ++i)
    {
        f>>a[i];
    }
    int t=0;
    for(int i=1; i<n; ++i)
    {
        for(int j=i+1; j<=n; ++j)
        {
            if(a[i]>a[j])
            {
                swap(a[i],a[j]);
            }
        }
    }
    for(int i=1; i<n; ++i)
    {
        for(int j=i+1; j<=n; ++j)
        {
            st=j+1;
            dr=n;
            sol=-1;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(a[mij]<=a[i]+a[j])
                {
                    st=mij+1;
                    sol=mij;
                }
                else dr=mij-1;
            }
            if(sol!=-1) t+=(sol-j);
        }
    }
    g<<t;
    return 0;
}