Cod sursa(job #3349984)

Utilizator Alberates10Albert Mates Alberates10 Data 4 aprilie 2026 11:38:55
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    ifstream fin("nrtri.in");
    ofstream fout("nrtri.out");
    int n;
    fin>>n;
    vector<int>v(n);
    for(int i=0;i<n;i++){
        fin>>v[i];
    }
    sort(v.begin(),v.end());
    int a,b,c,r=0;
    for(int i=0;i<n-2;i++){
        a=v[i];
        for(int y=i+1;y<n-1;y++){
            b=v[y];
            int s=a+b;
            int st=y+1,dr=n-1;
            while(st<=dr){
                int mij=st+((dr-st)/2);
                if(v[mij]<=s){
                    c=v[mij];
                    st=mij+1;
                }
                else{
                    dr=mij-1;
                }
            }
            //cout<<a<<" "<<b<<" "<<v[dr]<<" "<<dr-y<<endl;
            r+=dr-y;
        }
    }
    fout<<r;
    return 0;
}