Cod sursa(job #1768226)

Utilizator toadehuPuscasu Razvan Stefan toadehu Data 30 septembrie 2016 15:58:56
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <fstream>
#include <iostream>
#include <algorithm>

using namespace std;

int v[810],sum=0;

int main()
{
    ifstream fin ("nrtri.in");
    ofstream fout ("nrtri.out");
    int n;
    fin>>n;
    for (int i=1;i<=n;++i)
    {
        fin>>v[i];
    }
    sort (v,v+n);
    for (int i=1;i<n;++i)
    {
        for (int j=i+1;j<n;++j)
        {
            int x=v[i]+v[j];
            int s=j+1,d=n,mij=(d+s)/2;
            while (s<d)
            {
                mij=(d+s)/2;
                if (v[mij]>x)
                {
                    d=mij-1;
                }
                else
                    {
                        if (v[mij]==x){break;}
                        else
                        {
                            if (v[mij]<x)
                            {
                                s=mij+1;
                            }
                        }
                    }
            }
            if (v[mij]<=x)
            {
            //cout<<s<<"  "<<d<<"  "<<mij<<"  "<<i<<"<--->"<<j<<endl;
            sum=sum+mij-j;
            }
        }
    }
    fout<<sum;
}