Cod sursa(job #744329)

Utilizator padreatiAurelian Tutuianu padreati Data 8 mai 2012 13:26:00
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string.h>

using namespace std;

void sol();

int main() {
#ifdef PADREATI
    freopen("in.txt", "r", stdin);
#else
    freopen("nrtri.in", "r", stdin);
    freopen("nrtri.out", "w", stdout);
#endif
    sol();
    return 0;
}

#define N 801
#define LEN 30001

int n;
int v[N];

int cmp(const void *o1, const void *o2) {
    return *(int*) o1 - *(int*) o2;
}

void sol() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &v[i]);
    qsort(v, n, sizeof (int), cmp);
    int cnt[LEN];
    int pos = 0;
    for (int i = 1; i < LEN; i++) {
        if (v[pos] == i && pos < n) {
            cnt[i] = pos;
            pos++;
        } else {
            cnt[i] = cnt[i - 1];
        }
    }
    int t = 0;
    for (int i = 1; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (i > cnt[v[j] - v[i]])
                t += i - cnt[v[j] - v[i]];
    printf("%d\n", t);
}