Cod sursa(job #2618290)

Utilizator phayzeeeLeonard Vlaicu phayzeee Data 24 mai 2020 16:38:58
Problema Numarare triunghiuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
// Numarare_Triunghiuri.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

std::ifstream in("nrtri.in");
std::ofstream out("nrtri.out");

int main() {
	std::vector<long long>array;
	long long n, elem;

	in >> n;
	for (auto i = 0; i < n; i++) {
		in >> elem;
		array.push_back(elem);
	}

	long long counter = 0;
	std::sort(array.begin(), array.end());
	
	for (auto i = array.size() - 1; i >= 1; i--) {
		long long l = 0, r = i - 1;
		while (l < r) {
			if (array[l] + array[r] > array[i]) {
				counter += r - l;
				r -= 1;;
			}

			else {
				l += 1;
			}
		}
	}

	out << counter;

	return 0;
}