Cod sursa(job #2700169)

Utilizator bubblegumixUdrea Robert bubblegumix Data 26 ianuarie 2021 18:36:41
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include<fstream>
#include<vector>
#include<iterator>
#include<algorithm>
using namespace std;

int n;
ifstream f("nrtri.in");
ofstream g("nrtri.out");

int main()
{
    vector<int> a;
    f >> n;
    for (int i = 1; i <= n; i++)
    {
        int x;
        f>> x;
        a.push_back(x);
    }
    sort(begin(a), end(a));
    int total = 0;

    for (vector<int>::iterator i = begin(a); i != end(a) - 2; i++)
        for (vector<int>::iterator j = i + 1; j != end(a) - 1; j++)
        {
            vector<int>::iterator it = upper_bound(j + 1, end(a), *i + *j);

            if ((*(it - 1) <= *i + *j) && j != it - 1)
            {
                total += it - j - 1;
               // << *i << " " << *j << " " << *(it - 1) << " ---- " << total << endl;
            }

        }
    g << total;
    return 0;

}