Cod sursa(job #2939475)

Utilizator ax_dogaruDogaru Alexandru ax_dogaru Data 13 noiembrie 2022 18:45:47
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("nrtri.in");
ofstream fout("nrtri.out");

int main()
{
    int n, v[800], cnt=0, l, r, sol, mid;
    fin >> n;
    for(int i=0; i<n; i++) {
        fin >> v[i];
    }
    sort(v, v+n);
    for(int i=0; i<n-1; i++) {
        for(int j=i+1; j<n; j++) {
            sol=j;
            l=j+1;
            r=n-1;
            while(l<=r) {
                mid=(l+r)/2;
                //cout << mid << endl;
                if(v[mid]>v[i]+v[j]) {
                    r=mid-1;
                } else {
                    //cout << v[i] << " " << v[j] << " " << sol << endl;
                    sol=mid;
                    l=mid+1;
                }
            }
            //cout << " : " <<sol << endl;

            cnt=cnt+sol-j;
            //cout << cnt << sol <<endl;
        }
    }
    fout << cnt;
    return 0;
}