Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用BITSET C++检查点是否形成正方形_C++_Math - Fatal编程技术网

如何使用BITSET C++检查点是否形成正方形

如何使用BITSET C++检查点是否形成正方形,c++,math,C++,Math,位集是检查点是否形成正方形的最有效方法 读取对x,y的N个值,如何使用位集计算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 使用^2解决方案将是执行此操作的最有效方法 到目前为止,我的代码:我认为不正确: #include <iostream> #inclu

位集是检查点是否形成正方形的最有效方法

读取对x,y的N个值,如何使用位集计算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
使用^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;
}

如何使用std::bitset?欢迎使用堆栈溢出。你能分享一下你的尝试吗?我想到的解决方案是n²logn…我不知道如何使用位集,但我确信它比我构建代码的方式更好。我只是同时读两对,然后我计算形成正方形的两个点。然后我使用二进制搜索来检查这些点是否存在。如果是的话,我不能保存面积。问题是这是一个在^2*logV上的文件,有点慢。请在此处共享您的完整代码,它将帮助我们了解您尝试执行的操作。