Cod sursa(job #3295530)

Utilizator itachi_uchihaDarius itachi_uchiha Data 6 mai 2025 15:18:52
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#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;
}