【二分查找】-POJ-2002-Squares

2024-09-03 22:18
文章标签 二分 查找 poj 2002 squares

本文主要是介绍【二分查找】-POJ-2002-Squares,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:http://poj.org/problem?id=2002

题目描述:给出平面上若干个点,问能最多构成几个不重复的正方形。

解题思路:

第一反应是标记数组直接搜,好吧,内存超限。然后想了BFS或者DFS,太没前途了。然后想了哈希,不失为一种方法,但是不会操作。好吧还是按照九野大神选题的初衷来做吧——二分查找,为了锻炼自己,嗯!手写!好吧,我写的只是二分找 x 坐标,y 坐标没二分(没想出来。。),直接遍历了,反正也做出来了,而且跑了1000+ms,不错了。后来看了一下 STL 做法,代码也贴上来,以后就用 STL 了。

AC代码:

(看这千疮百孔的注释 _(#`O′) !!!

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>using namespace std;typedef struct node
{int x;int y;
};int num;
node stars[1200];bool cmp(node a,node b)
{if(a.x==b.x)return a.y>b.y;return a.x>b.x;
}int found(node p)
{//cout<<"want: "<<p.x<<" "<<p.y<<endl;int i;/*for(i=0;i<num;i++)if(stars[i].x==p.x&&stars[i].y==p.y)return 1;return 0;*/int l=0,r=num-1,flag=0,fd=-1;while(stars[l].x>=p.x&&stars[r].x<=p.x){//cout<<l<<" "<<r<<endl;if(stars[l].x==p.x){fd=l;break;}if(stars[r].x==p.x){fd=r;break;}if(l+1==r)break;int mid=(l+r)/2;if(stars[l].x>=p.x&&stars[mid].x<=p.x){r=mid;}else{l=mid;}}if(fd!=-1){//cout<<2<<endl;if(stars[fd].y>p.y){while(stars[fd].x==p.x){if(stars[fd].y==p.y){/*cout<<"找到了"<<endl;*/return 1;}fd++;}}else{while(stars[fd].x==p.x){if(stars[fd].y==p.y){/*cout<<"找到了"<<endl;*/return 1;}fd--;}}}return 0;
}int main()
{//freopen("2002_input.txt","r",stdin);//freopen("2002_output.txt","w",stdout);int i,j;while(scanf("%d",&num),num){int ans=0;for(i=0;i<num;i++){scanf("%d%d",&stars[i].x,&stars[i].y);}sort(stars,stars+num,cmp);
/*for(i=0;i<num;i++){cout<<i<<endl;cout<<stars[i].x<<" "<<stars[i].y<<endl;}cout<<endl;
*/node p1,p2;for(i=0;i<num;i++){for(j=i+1;j<num;j++){
/*cout<<"now: "<<endl;cout<<stars[i].x<<" "<<stars[i].y<<endl;cout<<stars[j].x<<" "<<stars[j].y<<endl;
*/p1.x=stars[i].x-(stars[i].y-stars[j].y);p1.y=stars[i].y-(stars[j].x-stars[i].x);p2.x=stars[j].x-(stars[i].y-stars[j].y);p2.y=stars[j].y-(stars[j].x-stars[i].x);
/*cout<<"want: "<<endl;cout<<p1.x<<" "<<p1.y<<endl;cout<<p2.x<<" "<<p2.y<<endl;
*/if(found(p1)&&found(p2))ans++;}}cout<<ans/2<<endl;}return 0;
}

[ STL 版本]

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>using namespace std;typedef struct node
{int x;int y;
};int num;
node stars[1200];bool cmp(node a,node b)
{if(a.x==b.x)return a.y>b.y;return a.x>b.x;
}int main()
{//freopen("2002_input.txt","r",stdin);//freopen("2002_output.txt","w",stdout);int i,j;while(scanf("%d",&num),num){int ans=0;for(i=0;i<num;i++){scanf("%d%d",&stars[i].x,&stars[i].y);}sort(stars,stars+num,cmp);
/*for(i=0;i<num;i++){cout<<i<<endl;cout<<stars[i].x<<" "<<stars[i].y<<endl;}cout<<endl;
*/node p1,p2;for(i=0;i<num;i++){for(j=i+1;j<num;j++){
/*cout<<"now: "<<endl;cout<<stars[i].x<<" "<<stars[i].y<<endl;cout<<stars[j].x<<" "<<stars[j].y<<endl;
*/p1.x=stars[i].x-(stars[i].y-stars[j].y);p1.y=stars[i].y-(stars[j].x-stars[i].x);p2.x=stars[j].x-(stars[i].y-stars[j].y);p2.y=stars[j].y-(stars[j].x-stars[i].x);
/*cout<<"want: "<<endl;cout<<p1.x<<" "<<p1.y<<endl;cout<<p2.x<<" "<<p2.y<<endl;
*/if(binary_search(stars,stars+num,p1,cmp)&&binary_search(stars,stars+num,p2,cmp))ans++;}}cout<<ans/2<<endl;}return 0;
}


AC截图:



这篇关于【二分查找】-POJ-2002-Squares的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1134197

相关文章

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

C#实现查找并删除PDF中的空白页面

《C#实现查找并删除PDF中的空白页面》PDF文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的,下面我们来看看如何使用Spire.PDFfor.NET通过C#... 目录安装 Spire.PDF for .NETC# 查找并删除 PDF 文档中的空白页C# 添加与删

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

usaco 1.2 Palindromic Squares(进制转化)

考察进制转化 注意一些细节就可以了 直接上代码: /*ID: who jayLANG: C++TASK: palsquare*/#include<stdio.h>int x[20],xlen,y[20],ylen,B;void change(int n){int m;m=n;xlen=0;while(m){x[++xlen]=m%B;m/=B;}m=n*n;ylen=0;whi

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M