Cod sursa(job #1721926)

Utilizator antracodRadu Teodor antracod Data 26 iunie 2016 19:05:33
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 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;
    for(; low<=high;)
    {
        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 mid;
}

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=0;i<n-2;i++)
    {
        for(j=i+1;j<n-1;j++)
        {
            x=BinarySearch(j+1,n-1,v[i]+v[j],v);
            if(v[i]+v[j]>=v[x])
                {
                    sum=sum+x-j;
                }
            else if(x-1!=j && v[i]+v[j]>=v[x-1])
            {
                sum=sum+x-1-j;
            }
        }
    }
    out<<sum;
}