Cod sursa(job #1792322)

Utilizator dcutitoiuCutitoiu Adrian-Nicolae dcutitoiu Data 30 octombrie 2016 12:33:18
Problema Numarare triunghiuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <string>
#include <iterator>
#include <numeric>
#include <string>
#include <set>
#include <map>
#include <queue>

using namespace std;

ifstream in("nrtri.in");
ofstream out("nrtri.out");

int main()
{
    //http://www.infoarena.ro/problema/nrtri
    int number;
    in >> number;

    vector<int> sticks(number);
    copy(istream_iterator<int>(in), istream_iterator<int>(), sticks.begin());

    sort(sticks.begin(), sticks.end());

    int total = 0;
    for(auto it = sticks.begin(); it != sticks.end(); it++)
        for(auto it2 = it + 1; it2 != sticks.end(); it2++)
        {
            int lower = *it2 - *it;
            int upper = *it + *it2;

            auto lb = lower_bound(it2 + 1, sticks.end(), lower);
            auto ub = upper_bound(it2 + 1, sticks.end(), upper);

            if(lb != sticks.end())
            {
                total += ub - lb;
            }
        }

    out << total;

    return 0;
}