Cod sursa(job #3324453)

Utilizator iustin251007iustin balint iustin251007 Data 22 noiembrie 2025 10:54:44
Problema Numarare triunghiuri Scor 100
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;

int main() {
    ifstream fin("nrtri.in");
    ofstream fout("nrtri.out");

    int n;
    fin >> n;
    int V[800];
    for (int i = 0; i < n; i++) fin >> V[i];

    sort(V, V + n);

    long long count = 0;

    for (int i = 0; i < n - 2; i++) {
        for (int j = i + 1; j < n - 1; j++) {
            int s = V[i] + V[j];
            int st = j + 1, dr = n - 1, pos = j;
            while (st <= dr) {
                int m = (st + dr) / 2;
                if (V[m] <= s) {
                    pos = m;
                    st = m + 1;
                } else {
                    dr = m - 1;
                }
            }

            count += (pos - j);
        }
    }

    fout << count << endl;
    return 0;
}