Cod sursa(job #1549175)

Utilizator GrandmasterSoucup Bogdan Grandmaster Data 11 decembrie 2015 23:53:06
Problema Numarare triunghiuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>
#include <map>
#include <iomanip>
#include <time.h>
#include <stdio.h>
//#include <iostream>
using namespace std;
ifstream cin("nrtri.in");
ofstream cout("nrtri.out");
int binar(int n, int x[], int elem, int st, int dr)
{
    int val = -1, cst = st, cdr = dr;
    while(st <= dr)
    {
        int m = (st + dr) / 2;
        if(x[m] == elem)
            val = m;
        if(x[m] <= elem)
            st = m + 1;
        else
            dr = m - 1;
    }
    if(val != -1)
        return val;
    return binar(n, x, x[st - 1], cst, cdr);
}
int main(){
    int n, x[803], k = 0;
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> x[i];
    sort(x, x + n);
    for(int i = 0; i < n; i++)
        for(int j = i + 1; j < n; j++){
            int m = binar(n, x, x[i] + x[j], 0, n - 1);
            k += m - j;
        }
    cout << k;
    return 0;
}