又来一道数论题,pku1971“Parallelogram Counting ”!

来源:互联网 发布:现流行的8位单片机 编辑:程序博客网 时间:2024/04/30 14:20

看来我的经验还是不足,我看同学写的代码,思路基本和我相同,他AC了,我却WA,郁闷ing。我把几个可能会错的函数注释掉了,不过很显然没错。。。

我的程序:

/*
Parallelogram Counting
Time Limit:5000MS  Memory Limit:65536K
Total Submit:1139 Accepted:323

Description
There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4-element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line.

Input
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases. It is followed by the input data for each test case.
The first line of each test case contains an integer n (1 <= n <= 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000.


Output
Output should contain t lines.
Line i contains an integer showing the number of the parallelograms as described above for test case i.


Sample Input


2
6
0 0
2 0
4 0
1 1
3 1
5 1
7
-2 -1
8 9
5 7
1 1
4 8
2 0
9 8


1

6
0 0
2 0
4 0
1 1
3 1
5 1
Sample Output


5
6


Source
Tehran Sharif 2004 Preliminary
*/

#include "stdio.h"
#include "vector"
#include "algorithm"
using namespace std;
typedef struct
{
 int x;
 int y;
 int i;
 int j;
}node;

bool cmp(node c1,node c2)
{
 /*if(c1.x>c2.x)
  return false;
 else if(c1.x==c2.x)
   if(c1.y>c2.y)
    return false;
 else
  return true;*/
 return (c1.x<c2.x)||(c1.x==c2.x&&c1.y<c2.y);
}
int main()
{
 int t,k,i,j,n,c,count,beg;
 int out[11];
 int xx[1000],yy[1000];
 node vec;
 scanf("%d",&t);
 if(t<1||t>10)
  return 0;
 k=t;
 while(t)
 {
  vector<node> vecs;
  c=0;
  count=0;
  scanf("%d",&n);
  if(n<1||n>1000)
   return 0;
  for(i=0;i<n;i++){
   scanf("%d %d",&xx[i],&yy[i]);
  }
  for(i=0;i<n;i++){
   for(j=i+1;j<n;j++){;
    vec.x=xx[i]-xx[j];
    vec.y=yy[i]-yy[j];
    vec.i=i;
    vec.j=j;
    if(vec.x<=0&&vec.y<=0){
     vec.x*=-1;
     vec.y*=-1;
    }
    if(vec.x<=0&&vec.y>=0){
     vec.x*=-1;
     vec.y*=-1;
    }
    vecs.push_back(vec);
    c++;
   }
  }
  sort(vecs.begin(),vecs.end(),cmp);
  /*for(i=0;i<c;i++){
   for(j=i+1;j<c;j++){    
    if(vecs[i].x==vecs[j].x&&vecs[i].y==vecs[j].y){
     if(!((xx[vecs[j].j]-xx[vecs[i].i])*vecs[i].y==(yy[vecs[j].j]-yy[vecs[i].i])*vecs[i].x))
      count++;
    }
   }
  }*/
  beg=0;
  while(beg < c)
  {
            for(i=beg; vecs[i].x==vecs[beg].x&&vecs[i].y==vecs[beg].y;i++)
                 for(j=i+1;vecs[j].x==vecs[beg].x&&vecs[j].y==vecs[beg].y;j++ )         
      if(!((xx[vecs[j].j]-xx[vecs[i].i])*vecs[i].y==(yy[vecs[j].j]-yy[vecs[i].i])*vecs[i].x))
       count++;
   beg = i;
  }
  out[k-t]=count;
  t--;
 }
 for(i=0;i<k;i++)
  printf("%d/n",out[i]/2); 
 return 0;
}

同学的程序:


#include <iostream>
#include <algorithm>
#include <memory>
using namespace std;
struct point
{   
 int x,y;
};
struct seg
{
 int a,b;   
 point v;
};
bool operator <(seg a,seg b)
{
    return (a.v.x<b.v.x)||(a.v.x==b.v.x&&a.v.y<b.v.y);
}   
seg tmp, vs[500500];
point p[1000];
bool commen(int i,int j);
int main(){
  int i,j,k,l,n, t;
  cin>>t;
  for(k = 0 ; k < t ;k ++)
  {
     memset(vs,0,sizeof(vs));
     cin>>n;
     for(i = 0 ;i < n ; i ++)    cin>>p[i].x>>p[i].y;
     int num=0;
     for(i = 0 ;i < n-1 ; i ++)
          for(j = i +1 ;j < n ;j ++)
    {
             tmp.a = i;    tmp.b = j;
             tmp.v.x = p[i].x-p[j].x;     tmp.v.y = p[i].y-p[j].y;
             if(tmp.v.x<=0&&tmp.v.y<=0)
    {
                 tmp.v.x=-tmp.v.x; tmp.v.y=-tmp.v.y;
             }
             if(tmp.v.x<=0&&tmp.v.y>=0)
    {
                 tmp.v.x=-tmp.v.x;   tmp.v.y=-tmp.v.y;
             } 
             vs[num++]=tmp;
          }      
    sort(vs,vs+num);
       int sum = 0;
       int beg = 0;
       while(beg < num)
    {
              for(i = beg; vs[i].v.x==vs[beg].v.x&&vs[i].v.y==vs[beg].v.y;i++)
                for(j = i+1;vs[j].v.x==vs[beg].v.x&&vs[j].v.y==vs[beg].v.y;j++ )         
     if(!commen(i,j))sum++;
    beg = i;
       }
       cout<<sum/2<<endl;
  }    
  return 0;
}
bool commen(int i,int j)
{
    if((p[vs[j].b].x-p[vs[i].a].x)*vs[i].v.y==(p[vs[j].b].y-p[vs[i].a].y)*vs[i].v.x) 
  return 1;
    return 0;
}

为什么呢?我快崩溃了! 

原创粉丝点击