Cod sursa(job #2609789)

Utilizator Stefan_RaduStefan Radu Stefan_Radu Data 3 mai 2020 15:48:51
Problema Numarare triunghiuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
// By Stefan Radu

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <string>
#include <cctype>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <map>
#include <set>

using namespace std;

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

#define sz(x) (int)(x).size()

typedef pair < int, int > pii;
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

int main() {

#ifdef STEF
  freopen("input", "r", stdin);
  freopen("output", "w", stdout);
#endif

  ios::sync_with_stdio(false);
  cin.tie(0);cout.tie(0);

  int n; cin >> n;
  vector < int > lengths(n);
  vector < int > mars(60007);
  for (int i = 0; i < n; ++ i) {
    cin >> lengths[i];
    mars[lengths[i]] += 1;
  }

  for (int i = 1; i < sz(mars); ++ i) mars[i] += mars[i - 1];

  int cnt = 0;
  for (int i = 0; i < n; ++ i) {
    for (int j = i + 1; j < n; ++ j) {

      int a = abs(lengths[i] - lengths[j]) - 1;
      int b = lengths[i] + lengths[j];

      cnt += mars[b] - mars[a];

      if (a < min(lengths[i], lengths[j])) {
        cnt -= 2;
      }
      else {
        cnt -= 1;
      }
    }
  }

  cout << cnt / 3 << '\n';
}