Pagini recente » Cod sursa (job #230825) | Cod sursa (job #550235) | Cod sursa (job #950407) | Cod sursa (job #3131351) | Cod sursa (job #3280607)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("nrtri.in");
ofstream fout("nrtri.out");
int a[1001], n;
int CautBin(int st, int dr, int x)
{
int mij, sol = 0;
if (x < a[st]) return 0;
if (x >= a[dr]) return n;
while (st <= dr)
{
mij = (st + dr) / 2;
if (a[mij] <= x)
{
sol = mij;
st = mij + 1;
}
else dr = mij - 1;
}
return sol;
}
int main()
{
int i, j, cnt, p;
fin >> n;
for (i = 1; i <= n; i++)
fin >> a[i];
sort(a + 1, a + n + 1);
cnt = 0;
for (i = 1; i < n - 1; i++)
for (j = i + 1; j < n; j++)
{
p = CautBin(j + 1, n, a[i] + a[j]);
if (p > 0) cnt += (p - j);
}
fout << cnt << "\n";
return 0;
}