Cod sursa(job #1461722)

Utilizator borcanirobertBorcani Robert borcanirobert Data 16 iulie 2015 12:48:56
Problema Numarare triunghiuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <fstream>
#include <algorithm>
using namespace std;

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

const int MAX = 815;
int N;
int a[MAX];
int t;
int poz1, poz2;
int lmax, lmin;
int maxim, minim;

int main()
{
    int i, j;

    fin >> N;
    for ( i = 1; i <= N; i++ )
    {
        fin >> a[i];
    }

    sort( a + 1, a + 1 + N );

    for ( i = 1; i <= N; i++ )
        for ( j = i + 1; j <= N; j++ )
        {
            poz2 = (upper_bound( a + 1, a + 1 + N, a[i] + a[j] ) - a) - 1;

            if ( a[N] <= a[i] + a[j] )
                poz2 = N;

            if ( poz2 > j )
            {
                t += poz2 - j;
            }
        }

    fout << t << '\n';

    fin.close();
    fout.close();
    return 0;
}