Cod sursa(job #3280161)

Utilizator Carnu_EmilianCarnu Emilian Carnu_Emilian Data 25 februarie 2025 17:46:20
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fcin("nrtri.in");
ofstream fcout("nrtri.out");

const int N = 805;
const int M = 9e4 + 5;
int v[N], f[M];
int n;

/**
a <= b + c
b <= a + c
abs(a - b) <= c <= a + b
c <= a + b
*/

int main()
{
    fcin >> n;
    for (int i = 1; i <= n; i++)
    {
        fcin >> v[i];
        f[v[i]]++;
    }
    for (int i = 1; i < M; i++)
        f[i] += f[i - 1];
    int rasp = 0;
    for (int i = 1; i < n; i++)
        for (int j = i + 1; j <= n; j++)
        {
            int d = max(0, abs(v[i] - v[j]) - 1);
            rasp += f[v[i] + v[j]] - 2 - f[d];
            if (v[i] <= d)
                rasp++;
            if (v[j] <= d)
                rasp++;
//    cout << i << ' ' << j << ' ' << rasp << endl;
        }
        rasp /= 3;
    fcout << rasp;
    return 0;
}