Pagini recente » Cod sursa (job #775707) | Rating Sirbu Radu-Mihai (radumihaisirbu) | Cod sursa (job #2459666) | Rating Carapencea Sabina-Elena (sabinochka) | Cod sursa (job #3295530)
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
ifstream fin("nrtri.in");
ofstream fout("nrtri.out");
int main() {
int N;
fin >> N;
vector<int> a(N + 1);
for (int i = 1; i <= N; ++i)
fin >> a[i];
sort(a.begin() + 1, a.begin() + N + 1);
int count = 0;
for (int i = 1; i <= N - 2; ++i) {
for (int j = i + 1; j <= N - 1; ++j) {
int sum = a[i] + a[j];
int left = j + 1, right = N, pos = -1 ;
while (left <= right) {
int mid = (left + right) / 2;
if (a[mid] <= sum) {
pos = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
if(pos != -1){
count += pos - (j+1) + 1;
}
}
}
fout << count << '\n';
return 0;
}