Cod sursa(job #44464)

Utilizator DastasIonescu Vlad Dastas Data 31 martie 2007 13:59:28
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 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 )
        {
            //bin search

            int p = j+1;
            int u = n;
            int s = a[j] + a[i];

            // 2 3 4 7

            while ( p < u )
            {
                int m = (p+u)/2;
                if ( s > a[m] )
                    p = m+1;
                else if ( s < a[m] )
                    u = m-1;
                else
                {
                    cnt += m-j+1;
                    break;
                }
            }
        }
    }
    out << cnt << endl;

    return 0;
}