Cod sursa(job #2062172)

Utilizator Teodor.mTeodor Marchitan Teodor.m Data 10 noiembrie 2017 03:07:39
Problema Numarare triunghiuri Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 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 - 1;
	int bot = st - 1;
	cerr << bot << " " << top << " ";
	while(st <= dr) {
		int mid = st + (dr - st) / 2;
		if(val >= a[mid]) {
			top = mid;
			st = mid + 1;
		}
		else {
			dr = mid - 1;
		}
	}
	cerr << bot << " " << top << '\n';
	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;
}