Cod sursa(job #170470)

Utilizator mihai_floreaFlorea Mihai Alexandru mihai_florea Data 2 aprilie 2008 19:48:21
Problema Triang Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <cstdio>
#include <math.h>
const double cos60=0.86602;
const double sin60=0.50000;
const double delta=0.001;
int n,nr;
double x[1501],y[1501];
int egal (double a,double b){
    return (fabs(a-b)<delta);
    }
void qsort(int ls,int ld){
     int i=ls,j=ld,mij=(ls+ld)/2;
     do {while (x[i]<x[mij] || (egal(x[i],x[mij]) && y[i]<y[mij])) i++;
         while (x[j]>x[mij] || (egal(x[j],x[mij]) && y[j]>y[mij])) j--;
         if (i<=j) {double aux=x[i];x[i]=x[j];x[j]=aux;
                    aux=y[i];y[i]=y[j];y[j]=aux;
                    i++;j--;}
         }
       while (i<=j);
     if (j>ls) qsort(ls,j);
     if (i<ld) qsort(i,ld);
     }
int gasit(int ls,int ld,double xx,double yy){
    int lo=ls,hi=ld,mij;
    while (lo<=hi) {
          mij=(lo+hi)/2;
          if (egal(x[mij],xx) && egal(y[mij],yy)) return 1;
          if (x[mij]>xx || (egal(x[mij],xx) && y[mij]>yy)) hi=mij-1;
          if (x[mij]<xx || (egal(x[mij],xx) && y[mij]<yy)) lo=mij+1;
          }
    return 0;
    }
int main(){
    int i,j,k;
    double xx,yy;
    freopen("triang.in","r",stdin);
    freopen("triang.out","w",stdout);
    scanf("%d",&n);
    for (i=1;i<=n;i++) scanf("%e %e",&x[i],&y[i]);
    qsort(1,n);
    nr=0;
    for (i=1;i<=n-1;i++) 
      for (j=i+1;j<=n;j++)
       {xx=x[i]*(1-cos60)+x[j]*cos60-y[i]*sin60+y[j]*sin60;
        yy=y[i]*(1-cos60)+y[j]*cos60-x[i]*sin60+x[j]*sin60;
        if (gasit(1,n,xx,yy)) nr++;
        xx=x[i]*cos60+x[j]*(1-cos60)-y[i]*sin60+y[j]*sin60;
        yy=y[i]*cos60+y[j]*(1-cos60)+x[i]*sin60-x[j]*sin60;
        if (gasit(1,n,xx,yy)) nr++;
        }
    printf("%d",nr/6);
   }