Cod sursa(job #44486)

Utilizator DastasIonescu Vlad Dastas Data 31 martie 2007 14:40:18
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int n;
int a[800];
int cnt = 0;

void read()
{
    in >> n;
    for ( int i = 0; i < n; ++i )
        in >> a[i];

    //sort

    for ( int i = 0; i < n-1; ++i )
        for ( int j = i+1; j < n; ++j )
            if ( a[i] > a[j] )
            {
                int t = a[i];
                a[i] = a[j];
                a[j] = t;
            }
}

int main()
{
    read();

    for ( int i = 0; i != n; ++i )
    {
        for ( int j = i+1; j != n; ++j )
        {
            int x = j+1;
            int y = n-1;
            int t = a[i] + a[j];
            int f;

            // 2 3 4 7

            while ( x <= y )
            {
                int m = (x+y)/2;
                if ( a[m] >= t )
                {
                    f = m;
                    y = m-1;
                }
                else
                    x = m+1;
            }

            if ( a[f] <= t )
                cnt += f-j;
            else if ( a[f] > t )
                cnt += f-j-1;
        }
    }

    out << cnt << endl;

    return 0;
}