Cod sursa(job #744624)

Utilizator padreatiAurelian Tutuianu padreati Data 9 mai 2012 11:42:36
Problema Numarare triunghiuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 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];


void sol() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &v[i]);
    int* cnt = (int*)calloc(LEN, sizeof(int));
    for (int i = 0; i < n; i++)  if(cnt[v[i]]==0) cnt[v[i]]=i+1;
    for (int i = LEN-1; i >=0; i--) cnt[i] = cnt[i]==0 ? cnt[i+1] : cnt[i];

    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]]-1)
                t += i - cnt[v[j] - v[i]]+1;
    printf("%d\n", t);
}