题目

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. Asp.net core 学习笔记 ( OData )

    2018-12-10 更新 : 从前我都是把 entity 直接用于 odata 曝露 api 给程序用. 如果这个程序是我们自己写的前端,这样的方式非常好,因为就好比前端可以直接对数据库每一个表做操 ...

  2. 优雅地记录Python程序日志2:模块组件化日志记录器

    本文摘自:https://zhuanlan.zhihu.com/p/32043593 本篇将会涉及: logging的各个模块化组件 构建一个组件化的日志器 logging的模块组件化 在上一篇文章中 ...

  3. python+selenium2(二)

    看完第一个程序,可能有不懂得地方,里面有定位元素的方式,之后会具体介绍定位的方式.这一篇介绍下对浏览器的操作. (1)浏览器的最大化   有点问题, Message: unknown error: c ...

  4. Linux 各种软件的安装-Jenkins和svn结合

    通常情况下,修改代码,打包,上传,发布,这么个顺序发布程序. 有了Jenkins这些重复性的操作可以统统交给程序自动处理.甚至可以指定发布的时机,例如提交一次svn就自动发布,每天定时发布等. 1:安 ...

  5. spring cloud: zuul(五): prefix访问前缀, ignoredServices粗粒度访问, yml不起作用

    路由的前缀 - 问题 zuul.prefix: 我们可以指定一个全局的前缀 strip-prefix: 是否将这个代理前缀去掉 zuul: prefix: /ecom 我的eureka:http:// ...

  6. ListView的简单使用--Android

    1.本例实现效果图 2.主要是activity_main.xml(布局文件)和Activity类文件,实现过程比较简单,直接附源码了哈! activity_main.xml: <?xml ver ...

  7. C#如何实现类似QQ那样靠边隐藏的功能

    http://www.cnblogs.com/yechensi/archive/2009/08/02/1537145.html C#如何实现类似QQ那样靠边隐藏的功能 你想过为自己的程序添加靠边隐藏的 ...

  8. vue基础 (三) 自动化工具(Vue CIL)

    一.自动化工具(Vue CIL) 安装过程 1. 先安装nvm 参考:https://www.jianshu.com/p/d0e0935b150a https://www.cnblogs.com/tj ...

  9. Fox And Dinner CodeForces - 510E (最大流)

    大意: n只狐狸, 要求分成若干个环, 每个环的狐狸不少于三只, 相邻狐狸年龄和为素数. 狐狸年龄都>=2, 那么素数一定为奇数, 相邻必须是一奇一偶, 也就是一个二分图, 源点向奇数点连容量为 ...

  10. Lowest Common Ancestor of a Binary Search Tree(Java 递归与非递归)

    题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...