Cod sursa(job #654247)

Utilizator dutzulBodnariuc Dan Alexandru dutzul Data 29 decembrie 2011 22:41:12
Problema Numarare triunghiuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("nrtri.in");
ofstream g("nrtri.out");
int i,j,n,s,x,a[805],nr,aux,k,ex;

int caut(int val)
{
        int poz,step,ok=0;
        for(step=1; step<n; step*=2);
        if (a[j+1]>val) return j;

        for(poz=j+1; step>0; step=step/2)
                if (step+poz<=n&&a[poz+step]<=val)
                        poz+=step,ok=1;
        return poz;
}

int main()
{

        f>>n;
        for(i=1; i<=n; i++) f>>a[i];
        sort(a+1,a+n+1);

        for(i=1; i<=n-2; i++)
                for(j=i+1; j<=n-1; j++) {
                        x=a[i]+a[j];
                        s+=caut(a[i]+a[j])-j;
                }

        g<<s;

        f.close();
        g.close();
        return 0;
}