Cod sursa(job #3229171)

Utilizator MihneaCircoMihnea Circo MihneaCirco Data 14 mai 2024 09:57:04
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include<fstream>
#include<algorithm>
using namespace std;
ifstream fin("nrtri.in");
ofstream fout("nrtri.out");
int v[801],cnt,l1,l2,x,aux;
int main()
{
    int n,st,dr,mij;
    fin>>n;
    for(int i=1;i<=n;i++)
        fin>>v[i];
    sort(v+1,v+n+1);
    for(int i=1;i<=n-2;i++)
    {
        l1=v[i];
        for(int j=i+1;j<=n-1;j++)
        {
            l2=v[j];
            x=v[i]+v[j];
            st=j; dr=n;
            while(st<=dr)
            {
                mij=(st+dr)/2;
                if(v[mij]<=x)
                {
                    aux=mij;
                    st=mij+1;
                }
                else
                    dr=mij-1;
            }
            cnt=cnt+aux-j;
        }
    }
    fout<<cnt;
}