Cod sursa(job #3251802)

Utilizator rst0605STAMULESCU ROBERT rst0605 Data 27 octombrie 2024 02:19:49
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	
	string s = "";
    ifstream fin(s + "nrtri.in");
    ofstream fout(s + "nrtri.out");

    int N;
    fin >> N;

    vector<int> sticks(N);
    for (int i = 0; i < N; ++i) {
        fin >> sticks[i];
    }

    // Sort the stick lengths
    sort(sticks.begin(), sticks.end());

    int count = 0;

    // Iterate over all triplets (i, j, k) with i < j < k
    for (int i = 0; i < N - 2; ++i) {
        for (int j = i + 1; j < N - 1; ++j) {
            for (int k = j + 1; k < N; ++k) {
                if (sticks[i] + sticks[j] >= sticks[k]) {
                    count++;
                } else {
                    break; // Since the array is sorted, no need to check further
                }
            }
        }
    }

    fout << count << endl;

    fin.close();
    fout.close();

    return 0;
}