Cod sursa(job #3362345)

Utilizator killah_queenTrifa Razvan killah_queen Data 6 august 2026 19:33:01
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("nrtri.in");
ofstream cout("nrtri.out");
int v[801];
int main()
{
    int n,total=0;
    bool ok=0;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>v[i];
    sort(v,v+n);
    for(int i=0;i<n-2;i++)
        for(int j=i+1;j<n-1;j++)
        {
            ok=0;
            int st=j+1,dr=n-1,mid,k=-1;
            int x=v[i]+v[j];
            while(st<=dr)
            {
                mid=st+(dr-st)/2;
                if(x>=v[mid])
                {
                    k=mid;
                    st=mid+1;
                }
                else dr=mid-1;
            }
            if(k!=-1)
                total=total+k-j;
        }
        cout<<total;
    return 0;
}