Cod sursa(job #757936)

Utilizator XladhenianGrigorita Vlad-Stefan Xladhenian Data 13 iunie 2012 19:55:03
Problema Trapez Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.75 kb

#include <fstream>
#include <cmath>
#include <string.h>
using namespace std;

struct TPoint;
typedef TPoint *PPoint;
struct TPoint
{
 long X,Y;
};

struct TLine;
typedef TLine *PLine;
struct TLine
{
 long i,j;
};

long N;
TPoint Puncte[1005];
TLine Linii[1000005];

int LineComp(const void *P1,const void *P2)
{
 PLine V1 = (PLine)(P1);
 PLine V2 = (PLine)(P2);
 long long dx1 = Puncte[V1->i].X - Puncte[V1->j].X;
 long long dx2 = Puncte[V2->i].X - Puncte[V2->j].X;
 long long dy1 = Puncte[V1->i].Y - Puncte[V1->j].Y;
 long long dy2 = Puncte[V2->i].Y - Puncte[V2->j].Y;
 long long r = (dy1 * dx2) - (dy2 * dx1);
 if (r == 0)
   {
    return 0;
   }
 if (r > 0)
   {
    return 1;
   }
 if (r < 0)
   {
    return -1;
   }
}

long eparalel(long l1,long l2)
{
 long long dx1 = Puncte[Linii[l1].i].X - Puncte[Linii[l1].j].X;
 long long dx2 = Puncte[Linii[l2].i].X - Puncte[Linii[l2].j].X;
 long long dy1 = Puncte[Linii[l1].i].Y - Puncte[Linii[l1].j].Y;
 long long dy2 = Puncte[Linii[l2].i].Y - Puncte[Linii[l2].j].Y;
 long long r = (dy1 * dx2) - (dy2 * dx1);
 if (r == 0)
   {
    return 1;
   }
 return 0;
}

int main(void)
{
 fstream fin("trapez.in",ios::in);
 fstream fout("trapez.out",ios::out);
 long i,j,c,r;
 fin >> N;
 for (i = 0;i < N;i += 1)
  {
   fin >> Puncte[i].X >> Puncte[i].Y;
  }
 c = 0;
 for (i = 0;i < N;i += 1)
  {
   for (j = i + 1;j < N;j += 1)
    {
     Linii[c].i = i;
     Linii[c].j = j;
     c += 1;
    }
  }
 qsort(Linii,c,sizeof(TLine),LineComp);
 r = 0;
 for (i = 0;i < c;)
  {
   j = i + 1;
   while ((eparalel(i,j)) && (j < c))
    {
     j += 1;
    }
   r += (((j - i) * (j - i - 1)) >> 1);
   i  = j;
  }
 fout << r;
 fin.close();
 fout.close();
 return 0;
}