题目

http://poj.org/problem?id=2002

题意

已知平面内有1000个点,所有点的坐标量级小于20000,求这些点能组成多少个不同的正方形。 

思路

如图,将坐标按照升序排列后,首先枚举p1,p2, 并判断p2是否在p1正下方或者左上角(因为每个正方形只有一条最右边或者是右下的边),按照下图计算p3,p4,判断p3,p4是否存在即可。

感想

排序时要注意和左上角这个信息相符,刚写完时用的是左下角,与升序排序不符合,会遗失部分正方形。

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 1000;
const int base = 2e4 + 4;
int n;
P a[maxn];
set<int> st; bool upperleft(P p1, P p2){
return p1.first <= p2.first && p1.second < p2.second;
} int adhere(P p){
return p.first * base + p.second;
} P othercorners(P p1, P p2){
int deltax = p2.first - p1.first;
int deltay = p2.second - p1.second;
P p3(deltay, -deltax);
P p4(deltax + deltay, deltay - deltax);
p3.first += p1.first; p4.first += p1.first;
p3.second += p1.second; p4.second += p1.second;
//printf("P1 (%d, %d), P2 (%d, %d), P3 (%d, %d), P4 (%d, %d)\n",
// p1.first, p1.second, p2.first, p2.second, p3.first, p3.second, p4.first, p4.second);
return P(adhere(p3), adhere(p4));
} int solve(){
int cnt = 0;
sort(a, a + n);
st.clear();
for(int i = 0;i < n;i++){
st.insert(adhere(a[i]));
}
for(int i = 0;i < n;i++){
for(int j = i + 1;j < n;j++){
if(upperleft(a[i], a[j])){
//printf("(%d, %d) is on the upper left corner of (%d, %d)\n", a[j].first, a[j].second, a[i].first, a[i].second);
P corners = othercorners(a[i], a[j]);
if(st.find(corners.first) != st.end() && st.find(corners.second) != st.end()){
cnt++;
}
}
}
}
return cnt;
} int main(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif // LOCAL
while(scanf("%d", &n) == 1 && n){
for(int i = 0;i < n;i++){
scanf("%d%d", &a[i].first, &a[i].second);
}
int ans = solve();
printf("%d\n",ans);
}
return 0;
}

POJ 2002 Squares 几何, 水题 难度: 0的更多相关文章

  1. UVa 10970 - Big Chocolate 水题 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  2. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  3. POJ 1840 Eqs 解方程式, 水题 难度:0

    题目 http://poj.org/problem?id=1840 题意 给 与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0 ...

  4. POJ 1936 All in All 匹配, 水题 难度:0

    题目 http://poj.org/problem?id=1936 题意 多组数据,每组数据有两个字符串A,B,求A是否是B的子串.(注意是子串,也就是不必在B中连续) 思路 设置计数器cnt为当前已 ...

  5. hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0

    H - National Day Parade Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  6. poj 2002 Squares 几何二分 || 哈希

    Squares Time Limit: 3500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 5749 Descript ...

  7. UVa 10340 - All in All 水题 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  8. UVa 3602 - DNA Consensus String 水题 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  9. UVa LA 3213 - Ancient Cipher 水题 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

随机推荐

  1. 前端实现实时通讯-----ajax长连接

    因为web运行模式为请求-响应,服务端无法主动发起通讯,所以通讯实时性存在各种问题,ajax轮询可以模拟及时通讯,但连接太频繁将给服务端带来很大压力,不频繁则实时性很差. 下面介绍在web上真正实现实 ...

  2. Codeforces 801B - Valued Keys

    B. Valued Keys 题目链接:http://codeforces.com/contest/801/problem/B time limit per test 2 seconds memory ...

  3. js实现文件的上传和输出,拖拽上传图片

    js文件上传 文件下载 以前的文件的下载都是在服务器,现在也是放在服务器比较好,有时候为了一些开发的方便,我们临时把处理的数据就放在了本地,然后自己访问.这个也是可以的. 1.利用html5的 Fil ...

  4. GrindEQ Math Utilities 2015破解版 图文安装和序列号补丁激活教程

    GrindEQ Math Utilities 2015破解版 图文安装和序列号补丁激活教程 https://www.sdbeta.com/mf/2018/1002/226048.html 软件下载: ...

  5. 怎么检测自己fastq的Phred类型 | phred33 phred64

    http://wiki.bits.vib.be/index.php/Identify_the_Phred_scale_of_quality_scores_used_in_fastQ # S - San ...

  6. 比较Class.getResource与Class.getClassLoader().getResource两种方式读取资源文件

    /** * @author zhangboqing * @date 2018/7/10 */ public class FileDemo { public static void main(Strin ...

  7. 20170912xlVBA批量导入txt文件

    Public Sub BatchImportTextFiles() AppSettings 'On Error GoTo ErrHandler Dim StartTime, UsedTime As V ...

  8. gem "searchkick"(4500✨) 智能搜索(使用Elastic search)(Gorails:建立一个侧边搜索栏)

    Searchkick

  9. uva11609

    以三个人组队为例,3组合是:C(3,0)=1,3,3,1.还有队长的选择.有 1*0,3*1,3*2,1*3种. 组合数:            1      3        3        1 ...

  10. 图解GIT,ZT

    图解GIT,ZT http://nettedfish.sinaapp.com/blog/2013/08/05/deep-into-git-with-diagrams/