Cod sursa(job #2357079)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 27 februarie 2019 09:26:31
Problema Numarare triunghiuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <bits/stdc++.h>
#define NMAX 801

using namespace std;

ifstream f ("nrtri.in");
ofstream g ("nrtri.out");

int n , i , ab , b , c , nr , j;
int a[NMAX];

bool ok(int mij){
    if(a[mij] <= ab + b && a[mij] + b >= ab && a[mij] + ab >=b)return true;
    return false;
}

int cb(int start){
    int st = start , dr = n , mij = 0 , poz = -1;
    while(st <= dr){
        mij = (dr - st) / 2 + st;
        if(ok(mij))poz = mij , dr = mij -1;
        else st = mij + 1;
    }
    return poz;
}

int main()
{
    f>>n;
    for(i=1;i<=n;i++)f>>a[i];
    sort(a+1,a+n+1);
    for(i=1;i<n;i++){
        ab = a[i];
        for(j=i+1;j<n;j++)
        {
            b = a[j];
            if(cb(j+1) != -1)nr++;
        }
    }
    g<<nr;
    return 0;
}