Cod sursa(job #2657253)

Utilizator Paul281881818818181991919191881818Draghici Paul Paul281881818818181991919191881818 Data 10 octombrie 2020 10:21:16
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.56 kb
#include<fstream>
#include<algorithm>
using namespace std;
ifstream cin("nrtri.in");
ofstream cout("nrtri.out");

int n,v[801];
int cautbin(int x){
	int high=n+1,mid,low=0;
    while (high - low > 1) {
        mid = (low + high) / 2;
        if (v[mid] <= x) {
            low = mid;
        } else { // v[mid] >= x
            high = mid;
        }
    }
    return low;
}
int main(){

cin>>n;
for(int i=1;i<=n;i++){
	cin>>v[i];
}
sort(v+1,v+n+1);
int sol=0;
for(int i=1;i<n;i++){
	for(int j=i+1;j<=n;j++){
	
		sol+=(cautbin(v[i]+v[j])-j);
	
	}
}
cout<<sol;
return 0;
}