POJ 2002(hash||二分,数学)
2016-04-22 15:56
411 查看
Squares
Description
A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however,
as a regular octagon also has this property.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x
and y coordinates.
Input
The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the
points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.
Output
For each test case, print on a line the number of squares one can form from the given stars.
Sample Input
Sample Output
Source
Rocky Mountain 2004
题意:给你n个点,这些点可以组成多少个正方形
题解:
http://blog.csdn.net/lyy289065406/article/details/6647405
直接四个点四个点地枚举肯定超时的,不可取。
普遍的做法是:先枚举两个点,通过数学公式得到另外2个点,使得这四个点能够成正方形。然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形。
但这种做法会使同一个正方形按照不同的顺序被枚举了四次,因此最后的结果要除以4.
已知: (x1,y1) (x2,y2)
则: x3=x1+(y1-y2) y3= y1-(x1-x2)
x4=x2+(y1-y2) y4= y2-(x1-x2)
或
x3=x1-(y1-y2) y3= y1+(x1-x2)
x4=x2-(y1-y2) y4= y2+(x1-x2)
据说是利用全等三角形可以求得上面的公式
有兴趣的同学可以证明下。。。
我是用map直接二分查找,跑了3350,题目3500,压线过了这一题
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(1e5)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
#ifdef CDZSC
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
set<pair<int,int> >s;
vector<pair<int, int> >v;
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt","w",stdout);
int _time_jc = clock();
#endif
//ios::sync_with_stdio(false);
int n,x,y;
while (~scanf("%d", &n)&&n)
{
v.clear();s.clear();
for (int i = 0; i < n; i++)
{
scanf("%d%d", &x, &y);
v.push_back(make_pair(x, y));
s.insert(make_pair(x, y));
}
pair<int, int>c, d;
int ans = 0;
for (int i = 0; i < v.size(); i++)
{
for (int j = i + 1; j < v.size(); j++)
{
int x1 = v[i].first;
int y1 = v[i].second;
int x2 = v[j].first;
int y2 = v[j].second;
int x3 = x1 + (y1 - y2);
int y3 = y1 - (x1 - x2);
int x4 = x2 + (y1 - y2);
int y4 = y2 - (x1 - x2);
if (s.count(make_pair(x3, y3)) && s.count(make_pair(x4, y4)))
ans++;
x3 = x1 - (y1 - y2);
y3 = y1 + (x1 - x2);
x4 = x2 - (y1 - y2);
y4 = y2 + (x1 - x2);
if (s.count(make_pair(x3, y3)) && s.count(make_pair(x4, y4)))
ans++;
}
}
printf("%d\n", ans/4);
}
#ifdef CDZSC
debug("time: %d\n", int(clock() - _time_jc));
#endif
return 0;
}
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 18453 | Accepted: 7101 |
A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however,
as a regular octagon also has this property.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x
and y coordinates.
Input
The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the
points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.
Output
For each test case, print on a line the number of squares one can form from the given stars.
Sample Input
4 1 0 0 1 1 1 0 0 9 0 0 1 0 2 0 0 2 1 2 2 2 0 1 1 1 2 1 4 -2 5 3 7 0 0 5 2 0
Sample Output
1 6 1
Source
Rocky Mountain 2004
题意:给你n个点,这些点可以组成多少个正方形
题解:
http://blog.csdn.net/lyy289065406/article/details/6647405
直接四个点四个点地枚举肯定超时的,不可取。
普遍的做法是:先枚举两个点,通过数学公式得到另外2个点,使得这四个点能够成正方形。然后检查散点集中是否存在计算出来的那两个点,若存在,说明有一个正方形。
但这种做法会使同一个正方形按照不同的顺序被枚举了四次,因此最后的结果要除以4.
已知: (x1,y1) (x2,y2)
则: x3=x1+(y1-y2) y3= y1-(x1-x2)
x4=x2+(y1-y2) y4= y2-(x1-x2)
或
x3=x1-(y1-y2) y3= y1+(x1-x2)
x4=x2-(y1-y2) y4= y2+(x1-x2)
据说是利用全等三角形可以求得上面的公式
有兴趣的同学可以证明下。。。
我是用map直接二分查找,跑了3350,题目3500,压线过了这一题
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
#define N int(1e5)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;
#ifdef CDZSC
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
set<pair<int,int> >s;
vector<pair<int, int> >v;
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt","w",stdout);
int _time_jc = clock();
#endif
//ios::sync_with_stdio(false);
int n,x,y;
while (~scanf("%d", &n)&&n)
{
v.clear();s.clear();
for (int i = 0; i < n; i++)
{
scanf("%d%d", &x, &y);
v.push_back(make_pair(x, y));
s.insert(make_pair(x, y));
}
pair<int, int>c, d;
int ans = 0;
for (int i = 0; i < v.size(); i++)
{
for (int j = i + 1; j < v.size(); j++)
{
int x1 = v[i].first;
int y1 = v[i].second;
int x2 = v[j].first;
int y2 = v[j].second;
int x3 = x1 + (y1 - y2);
int y3 = y1 - (x1 - x2);
int x4 = x2 + (y1 - y2);
int y4 = y2 - (x1 - x2);
if (s.count(make_pair(x3, y3)) && s.count(make_pair(x4, y4)))
ans++;
x3 = x1 - (y1 - y2);
y3 = y1 + (x1 - x2);
x4 = x2 - (y1 - y2);
y4 = y2 + (x1 - x2);
if (s.count(make_pair(x3, y3)) && s.count(make_pair(x4, y4)))
ans++;
}
}
printf("%d\n", ans/4);
}
#ifdef CDZSC
debug("time: %d\n", int(clock() - _time_jc));
#endif
return 0;
}
相关文章推荐
- 详解网络传输中的三张表,MAC地址表、ARP缓存表以及路由表
- linux常用指令
- 奋斗吧,程序员——第五章 行路难!行路难!多歧路,今安在
- JEE中事物的总结
- Maven之Spring_boot创建表结构
- gcc 命令
- 我的第一个博客
- A Knight's Journey(搜索题)
- 关于 linux ssh 的配置.
- SkyLine二次开发——解决在web页面启动时自动运行TerraExplorer的问题
- SpringMVC整合MongoDB开发 架构搭建
- android 开发中 添加库文件 和so 文件的存放位置和添加依赖
- Linux及安全期中总结
- 计算机图形学(二)输出图元_6_OpenGL曲线函数_3_椭圆生成算法
- Linux——作业1
- Java线程(九):Condition-线程通信更高效的方式
- Git备忘录
- 体育IP价值大爆发 本土赛事IP蕴含着巨大发展潜力
- caffe accuracy 学习
- 奋斗吧,程序员——第四章 人生若只如初见,何事秋风悲画扇