Cod sursa(job #2436251)

Utilizator Dragos1226Dragos Chileban Dragos1226 Data 5 iulie 2019 12:22:58
Problema Numarare triunghiuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <bits/stdc++.h>
using namespace std;

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

int binarysearch(int v[],int x,int low,int hi) {
    int m;
    if(low<=hi) {
        m=low + (hi-low)/2;
        if(v[m]>=x)
        return 1;
        else
        if(x>v[m]) return binarysearch(v,x,m+1,hi);
        else return binarysearch(v,x,low,m-1);}
        else return 0;
    }



int main() {
    int n,v[800],c=0;
    in>>n;
    for(int i=0;i<n;i++)
        in>>v[i];
    sort(v,v+n,greater<int>());
    for(int i=0;i<n-2;i++)
        for(int j=i+1;j<n-1;j++) {
        if(binarysearch(v,v[i]-v[j],j+1,n-1)) c++;
    }
    out<<c;
}