Cod sursa(job #1233461)

Utilizator rocandu16Badulescu Dan Andrei rocandu16 Data 25 septembrie 2014 15:14:33
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <algorithm>
bool cmp(int a, int b)
{
   if (a<b) return 1;
   else return 0;

}
using namespace std;
int v[800],i,j,n,s,end,beg,p,cnt=0,nr,ok,k;
int main()
{
    ifstream f("nrtri.in");
    ofstream g("nrtri.out");
    f>>n;
    for(i=1;i<=n;i++)
    {
        f>>v[i];

    }
    sort(&v[0],&v[n],cmp);
  /*  for(i=0;i<n;i++)
    {
       for(j=i+1;j<n;j++)
       {
        s=v[i]+v[j];
        beg=0;
        end=n-1;
        while(beg<end)
        {
          p=(beg+end)/2;
          if (s<v[p]) end=p-1;
          if (s>v[p]) beg=p+1;
          if (s==v[p]) beg=end=p;

        }
       // cnt+=p;
       if (p>=j) cnt+=p-2;
       else if (p>=i)  cnt+=p-1;
       else cnt+=p;
       }

    }*/
   for(int i=1; i<n-1; i++)
        for(j=i+1; j<n; j++){
            ok=1;
            k=j+1;
            while(ok==1 && k<=n){
            ok=0;
            if(v[i]+v[j]>=v[k]){
                cnt++;
                ok=1;
            }
            k++;
        }
    }
    g<<cnt;

    return 0;
}