Cod sursa(job #2062166)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 10 noiembrie 2017 02:47:11
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <bits/stdc++.h>
using namespace std;

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

int a[850];
int n;

int cautare_binara(int a[], int st, int dr, int val)
{
	int top = st;
	int bot = st;
	while(st <= dr) {
		int mid = st + (dr - st) / 2;
		if(val > a[mid]) {
			top = mid;
			st = mid + 1;
		}
		else {
			dr = mid - 1;
		}
	}
	return top - bot;
}
int main()
{
	fin >> n;

	for(int i = 1; i <= n; ++i) fin >> a[i];
	sort(a + 1, a + n + 1);
	for(int i = 1; i <= n; ++i) gout << a[i] << " ";
	gout << '\n';

	int ans = 0;
	for(int i  = 1; i < n; ++i) {
		for(int j = i + 1; j <= n; ++j) {
			int suma = a[i] + a[j];
			int pos = cautare_binara(a, j + 1, n, suma);
			
			ans += pos;;
		}
	}

	gout << ans;
	return 0;
}