Cod sursa(job #1721913)

Utilizator antracodRadu Teodor antracod Data 26 iunie 2016 18:38:14
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

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

int result;

int BinarySearch(int low,int high,int k,int v[16001])
{
    int mid,flag=0,nrtrans;
    for(; low<=high && flag==0;)
    {
        mid=(low+high)/2;
        if(v[mid]==k)
        {
            result=mid;
            high=mid-1;
        }
        else if(k>v[mid])
        {
            low=mid+1;
        }
        else
        {
            high=mid-1;
        }
    }

    return result;
}

int main()
{
    int v[16001];
    int i,j,p,n;
    int x,y,z;
    int sum=0;
    in>>n;
    for(i=0;i<n;i++)
    {
        in>>v[i];
    }
    sort(v,v+n);
    for(i=n-1;i>1;i--)
    {
        for(j=i-1;j>0;j--)
        {
            x=BinarySearch(0,j-1,v[i]-v[j],v);
            if(v[x]+v[j]>=v[i])
                {
                    sum=sum+j-x;
                }



        }
    }
    out<<sum;
}