Cod sursa(job #924003)

Utilizator andreiblaj17Andrei Blaj andreiblaj17 Data 24 martie 2013 00:24:21
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define nmax 805

using namespace std;

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

int n,v[nmax],k,hi,lo,mid;

void citire() {
    in >> n;
    for (int i=1; i<=n; i++) in >> v[i];
}

void sortare() {
    sort(v+1,v+n+1);
}

void hard_work() {
    for (int i=1; i<=n-2; i++)
        for (int j=i+1; j<=n-1; j++){
            hi=n+1;
            lo=j;
            while (hi-lo>1){
                mid=int((hi+lo)/2);
                if (v[i]+v[j]<=v[mid]) hi=mid;
                else lo=mid;
                if (v[lo]<=v[i]+v[j]) k+=lo-j;
            }
        }
    out << k;
}

int main()
{
    citire();
    sortare();
    hard_work();

    return 0;
}