题目

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. 力扣(LeetCode)453. 最小移动次数使数组元素相等

    给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1. 示例: 输入: [1,2,3] 输出: 3 解释: 只需要3次移动(注意每次移动 ...

  2. python静态方法、类方法

    常规: class Dog(object): def __init__(self,name): self.name=name def eat(self): print('%s is eating'%s ...

  3. CentOS7 下源代码安装php7

    安装PHP7: php-7.1.2.tar.gz:下载:wget http://cn2.php.net/get/php-7.1.2.tar.gz/from/this/mirror 安装php: # t ...

  4. Android实现选择题答题(包括单选、多选和答题卡)

    在线答题demo,具体代码是一年多前完成的,比较简单,不再贴出,请参见Github. 主要功能: 单选:点击选项直接进入下一题.多选:选择多个选项,向右滑动进入下一题.答题卡:点击题号重新进入答题界面 ...

  5. python-day73--django课上项目01

    from django.db import models # Create your models here. class Book(models.Model): name=models.CharFi ...

  6. 利用tablespace特性将数据库移动到新磁盘

    目前开发一台EC2的PostgreSQL服务器的磁盘空间已经严重不足,该磁盘非LVM,所以不考虑磁盘扩容方法,研发希望可以分区/data/02对应的/dev/xvdl1磁盘分担部分数据库的数据,这样也 ...

  7. python中列表生成式

    1.简介 列表生成式即List Comprehensions,是Python中用于创建list的生成式. 2.示例 [表达式  循环体  条件语句] #!/usr/bin/env python # - ...

  8. python 解析与生成xml

    xml.etree.ElementTree模块为xml文件的提取和建立提供了简单有效的API.下文中使用ET来代表xml.etree.ElementTree模块. XML是一种内在的分层的数据形式,展 ...

  9. SQL Server查询优化中的两个选项

    本文中,我们将介绍两个SQL Server中的可用概念,它们是使用SQL Server时值得注意的技术. 1.        OPTIMIZE FOR Unknown SQL Server 2005版 ...

  10. python dpkt解析ssl流

    用法:python extract_tls_flow.py -vr  white_pcap/11/2018-01-10_13-05-09_2.pcap  -o pcap_ssl_flow.txt  & ...