Cod sursa(job #413393)

Utilizator SpiderManSimoiu Robert SpiderMan Data 8 martie 2010 14:38:01
Problema Numarare triunghiuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
using namespace std;

#define hg 8192

int N,v[801],poz;
char ch[hg];

FILE *f=fopen("nrtri.in","r");
FILE *g=fopen("nrtri.out","w");

inline void cit (int &x)
{
    x = 0;
    if (ch[0]=='\0') fread (ch, 1, hg, f);
    else while (ch[poz] < '0' || ch[poz] > '9')
        if (++poz == hg)
            fread (ch, 1, hg, f), poz = 0;

    while (ch[poz] >= '0' && ch[poz] <= '9')
    {
        x = x * 10 + ch[poz] - '0';
        if (++poz == hg)
            fread (ch, 1, hg, f), poz = 0;
    }
}
inline int cb(int i,int j)
{
    int st,dr,mij,rez=0;
    for (st = j + 1,dr = N;st <= dr; )
    {
        mij = st + ((dr - st) >> 1);
        if (v[i] + v[j] >= v[mij]) rez=mij-j,st=mij + 1;
        else dr=mij - 1;
    }
    return rez;
}
void solve()
{
    int i,j,rez=0;
    cit(N);
    for (i=1;i<=N;i++) cit(v[i]);
    sort(v + 1,v + N + 1);
    for (i=1;i<=N-1;i++)
      for (j=i+1;j<=N;j++)
         rez+=cb(i,j);
    fprintf(g,"%d",rez);
}
int main()
{
    solve();
    return 0;
}