Cod sursa(job #1020327)

Utilizator ELHoriaHoria Cretescu ELHoria Data 1 noiembrie 2013 22:11:52
Problema Triang Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <cstdio>
#include <math.h>
#include <algorithm>
#include <functional>
#include <vector>
#define pdd pair<double,double>
#define M_PI  3.14159265358979323846
#define eps 1e-3
#define x first
#define y second
 
using namespace std;
 
const double s60 = sin(60 * M_PI / 180.0);   
const double c60 = cos(60 * M_PI / 180.0);
const int nmax = 1502;
int n;
pdd v[nmax];
 
inline pair<pdd,pdd> getPoints(pdd a,pdd b) {
    return make_pair(
        make_pair(c60 * (a.x - b.x) - s60 * (a.y - b.y) + b.x,
                  s60 * (a.x - b.x) + c60 * (a.y - b.y) + b.y),
        make_pair(c60 * (a.x - b.x) + s60 * (a.y - b.y) + b.x,
                  - s60 * (a.x - b.x) + c60 * (a.y - b.y) + b.y ));
}
 
 
inline int comp(const double &a,const double &b) {
    if(b - a < eps && b - a > - eps) return 0;
    if(b - a > eps) return -1;
    return -1;
}
 
inline void readData() {
    scanf("%d",&n);
    for(int i = 0;i < n;i++) {
        scanf("%lf %lf",&v[i].x,&v[i].y);
    }
}
 
inline bool search(pdd p) {
    int pos = 0;
    for(int step = 1<<11;step > 0;step >>= 1) {
        if(pos + step < n && comp(p.x,v[pos + step].x) <= 0) {
            pos += step;
        }
    }
 
    for(int step = 1<<11;step > 0;step >>= 1) {
        if(pos + step < n && comp(p.y,v[pos + step].y) <= 0) {
            pos += step;
        }
    }
    return (comp(p.x,v[pos].x) == 0 && comp(p.y,v[pos].y) == 0);
}
 
inline int solve() {
    int ret = 0;
    sort(v,v + n);
    for(int i = 0;i < n;i++) {
        for(int j = i + 1;j < n;j++) {
            pair< pdd ,pdd > points = getPoints(v[i],v[j]);
            ret += search(points.x);
            ret += search(points.y);
        }
    }
    return ret;
}
 
int main()
{
    freopen("triang.in","r",stdin);
    freopen("triang.out","w",stdout);
    readData();
    printf("%d\n",solve());
    return 0;
}