Bitset是检查点是否形成Square的最有效方法。
读取对(x,y)的N值,使用bitset计算N对形成的平方数(和最大面积)的最佳方法是什么?
输入:
9
5 3
1 4
1 3
1 2
2 1
2 3
3 4
3 2
4 2
输出:
3 -> Number of squares formed by the pairs x,y
5 -> biggest area
O(N ^ 2)解决方案是最有效的方法吗?
我的代码到目前为止:我认为不正确:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
struct c{
int x,y;
}A,B,coordenadas[3001],R[4];
struct cc{
long int c1,c2;
}R1,R2;
int quadrados=0,n=0;
long int area;
long int concat[3001];
int bs(long int search)
{
int first = 0;
int last = n - 1;
int middle = (first+last)/2;
while( first <= last )
{
if ( concat[middle] < search )
first = middle + 1;
else if ( concat[middle] == search )
{
return 1;
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
return 0;
}
void calculo()
{
int x=0,y=0,a=0,b=0,limpa=0;
if(A.y==B.y)
{
x=abs(A.x-B.x);
area=x*x;
R1.c1=(B.y + x) * 100000 + (A.x);
R1.c2=(B.y - x) * 100000 + (A.x);
//R2.c1=(B.y + x) * 100000 + (B.x);
//R2.c2=(B.y - x) * 100000 + (B.x);
if((int)bs(R1.c1)==1 && (int)bs(R1.c2)==1)
quadrados+=1;
else
area=0;
}
else
if(A.x==B.x)
{
x=abs(B.y-A.y);
area=x*x;
R1.c1=(B.y) * 100000 + (A.x + x);
R1.c2=(B.y) * 100000 + (A.x - x);
//R2.c1=(A.y) * 100000 + (B.x + x);
//R2.c2=(A.y) * 100000 + (B.x - x);
if((int)bs(R1.c1)==1 && (int)bs(R1.c2)==1)
quadrados+=1;
else
area=0;
}
else
if(A.y>B.y)
{
x=abs(B.x-A.x);
y=abs(B.y-A.y);
area=sqrt(x*x+y*y)*sqrt(x*x+y*y);
R1.c1=(A.y + x) * 100000 + (A.x + y);
R1.c2=(B.y + x) * 100000 + (B.x + y);
//R2.c1=(A.y - x) * 100000 + (A.x - y);
//R2.c2=(B.y - x) * 100000 + (B.x - y);
if((int)bs(R1.c1)==1 && (int)bs(R1.c2)==1)
quadrados+=1;
else
area=0;
}
else
if(A.y<B.y)
{
x=abs(B.y-A.y);
y=abs(B.x-A.x);
area=sqrt(x*x+y*y)*sqrt(x*x+y*y);
R1.c1=(B.y + y) * 100000 + (B.x - x);
R1.c2=(A.y + y) * 100000 + (A.x - x);
//R2.c1=(B.y - y) * 100000 + (B.x + x);
//R2.c2=(A.y - y) * 100000 + (A.x + x);
if((int)bs(R1.c1)==1 && (int)bs(R1.c2)==1)
quadrados+=1;
else
area=0;
}
// printf("Area=%ld e R0-> (%d,%d) e R1 (%d,%d) R2 (%d,%d) R3 (%d,%d)\n",area,R[0].x,R[0].y,R[1].x,R[1].y,R[2].x,R[2].y,R[3].x,R[3].y);
}
int main() {
int M=1,mArea=0;
cin >> n;
for(int i=0;i<n;i++)
{cin >> coordenadas[i].x >> coordenadas[i].y;concat[i]=coordenadas[i].y * 100000 + coordenadas[i].x;}
for(int i=0;i<n;i++)
{
A.x=coordenadas[i].x;A.y=coordenadas[i].y;
for(int ii=M;ii<n;ii++)
{
B.x=coordenadas[ii].x;B.y=coordenadas[ii].x;
calculo();
if(mArea<area)
mArea=area;
}
M+=1;
}
if(quadrados==0)
cout << quadrados << endl;
else
cout << quadrados << endl << mArea << endl;
return 0;
}