Cod sursa(job #2609792)

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

#include <fstream>
#include <algorithm>

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;

const int MAX_CNT = 30001, MAX_N = 800;
int mars[MAX_CNT], lengths[MAX_N];

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;
  for (int i = 0; i < n; ++ i) {
    cin >> lengths[i];
    mars[lengths[i]] += 1;
  }

  sort(lengths, lengths + n);

  for (int i = 1; i < MAX_CNT; ++ 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 = max(0, lengths[j] - lengths[i] - 1);
      int b = min(lengths[i] + lengths[j], 30000);

      cnt += mars[b] - mars[a];
      cnt -= a < lengths[i] ? 2 : 1;
    }
  }

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