POJ 2002 Squares 几何, 水题 难度: 0
题目
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的更多相关文章
- UVa 10970 - Big Chocolate 水题 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- POJ 1840 Eqs 解方程式, 水题 难度:0
题目 http://poj.org/problem?id=1840 题意 给 与数组a[5],其中-50<=a[i]<=50,0<=i<5,求有多少组不同的x[5],使得a[0 ...
- POJ 1936 All in All 匹配, 水题 难度:0
题目 http://poj.org/problem?id=1936 题意 多组数据,每组数据有两个字符串A,B,求A是否是B的子串.(注意是子串,也就是不必在B中连续) 思路 设置计数器cnt为当前已 ...
- hdu 3687 10 杭州 现场 H - National Day Parade 水题 难度:0
H - National Day Parade Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- poj 2002 Squares 几何二分 || 哈希
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 5749 Descript ...
- UVa 10340 - All in All 水题 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 3602 - DNA Consensus String 水题 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVa LA 3213 - Ancient Cipher 水题 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
随机推荐
- 学习笔记49—matlab FDR校正
matlab自带函数mafdr,当ttest数较多时,可直接用[FDR, Q]=mafdr(P):但是Storey procedure在p值少于1000个时会崩溃,此时应改用BH FDR方法:mafd ...
- (转)Qt中图片相对位置的引用
原文作者:locky1218 原文地址:https://blog.csdn.net/locky1218/article/details/9749703/ 一般使用相对位置的时候可能无法显示图片,可 ...
- git分支错误提交导致代码丢失--窗口提示HEAD detached错误
今天开发时git 检出分支到本地时操作错误,导致在一个临时分支上开发,且把代码提交了,结果代码未提交到任何分支,提交时还报了个错: HEAD detached at 4d927fa4 后来把代码重新检 ...
- (转)C#连接OleDBConnection数据库的操作
对于不同的.net数据提供者,ADO.NET采用不同的Connection对象连接数据库.这些Connection对我们屏蔽了具体的实现细节,并提供了一种统一的实现方法. Connection类有四种 ...
- Go语言学习之2 包、函数、常量、数据类型、字符操作
第一部分:基本数据类型和操作符 1. 文件名&关键字&标识符 (1)所有go源码以.go结尾 (2)标识符以字母或下划线开头,大小写敏感,比如: a. boy b. Bo ...
- Bayesian generalized linear model (GLM) | 贝叶斯广义线性回归实例
一些问题: 1. 什么时候我的问题可以用GLM,什么时候我的问题不能用GLM? 2. GLM到底能给我们带来什么好处? 3. 如何评价GLM模型的好坏? 广义线性回归啊,虐了我快几个月了,还是没有彻底 ...
- Python自学:第二章 数字 整数
>>>2 + 3 5 >>>3 - 2 1 >>>3 * 2 6 >>>3 / 2 1.5
- 表结构中updated_time设计为ON UPDATE CURRENT_TIMESTAMP时,使用过程的一个坑
一.mysql表结构中存在如下设计时 表结构中updated_time设计为ON UPDATE CURRENT_TIMESTAMP时,如下 `updated_time` datetime NOT NU ...
- LeetCode--441--排列硬币
问题描述: 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围 ...
- [转载]mapreduce合并小文件成sequencefile
mapreduce合并小文件成sequencefile http://blog.csdn.net/xiao_jun_0820/article/details/42747537