codeforces 425D Sereja and Squares n个点构成多少个正方形
输入n个点,问可以构成多少个正方形。n,xi,yi<=100,000。
刚看题的时候感觉好像以前见过╮(╯▽╰)╭最近越来越觉得以前见过的题偶尔就出现类似的,可是以前不努力啊,没做出来的没认真研究
首先想到的朴素是n^2的算法,10^10显然不行=。=抱着过预判的侥幸心理写了一发,打算过预判之后锁上看别人代码怎么做,结果TLE 13
第二天早上起来看题解,看不懂,看别人的代码才懂的。。。。
大概就是,避开最坏情况,枚举正方形左下角的点,然后如果当前x的点数量比sqrt(n)要小,就用当前x的上边的点做正方形的左上角,再检查另外2个点是否存在就好了;如果当前x的点数量比sqrt(n)要大,就用当前y的右边的点做正方形的右下角,再检查。
复杂度大概就是nlogn吧
拖了这么久终于写下第一篇博客日志了~~~
哪里有错的话求指正。。。。。
Note:正方形要平行于坐标轴
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <set>
using namespace std; #define ll long long
#define inf 0x3f3f3f3f
#define eps 1e-8 const int maxn = ;
const int sqrtmaxn = (int)sqrt(maxn+.) + ;
vector<int>vec[maxn];
//set<int>st[maxn*2];
inline bool find(int x,int y){
if(x>=maxn)return false;
return binary_search(vec[x].begin(),vec[x].end(),y);
}
int main(){
int n;
while(~scanf("%d",&n)){
//for(int i=0;i<maxn;++i)vec[i].clear(),st[i].clear();
for(int i=;i<n;++i){
int x,y;
scanf("%d%d",&x,&y);
vec[x].push_back(y);
//st[x].insert(y);
}
for(int i=;i<maxn;++i)sort(vec[i].begin(),vec[i].end());
ll ans=;
for(int x=;x<maxn;++x){
if(vec[x].size()<sqrtmaxn){
for(int i=;i<vec[x].size();++i){
for(int j=i+;j<vec[x].size();++j){
int d=vec[x][j]-vec[x][i];
if(find(x+d,vec[x][i]) && find(x+d,vec[x][j]))++ans;
}
}
}
else {
for(int xx=x+;xx<maxn;++xx){
for(int i=;i<vec[xx].size();++i){
int yy=vec[xx][i],d=xx-x;
if(find(x,yy) && find(x,yy+d) && find(xx,yy+d))++ans;
}
}
}
}
printf("%I64d\n",ans);
}
return ;
}
codeforces 425D Sereja and Squares n个点构成多少个正方形的更多相关文章
- codeforces 314E Sereja and Squares
discription Sereja painted n points on the plane, point number i (1 ≤ i ≤ n) has coordinates (i, 0). ...
- Codeforces.314E.Sereja and Squares(DP)
题目链接 http://www.cnblogs.com/TheRoadToTheGold/p/8443668.html \(Description\) 给你一个擦去了部分左括号和全部右括号的括号序列, ...
- CF 314 E. Sereja and Squares
E. Sereja and Squares http://codeforces.com/contest/314/problem/E 题意: 给你一个擦去了部分左括号和全部右括号的括号序列,括号有25种 ...
- Codeforces 314 E. Sereja and Squares
http://codeforces.com/contest/314/problem/E 题意: 原本有一个合法的括号序列 擦去了所有的右括号和部分左括号 问有多少种填括号的方式使他仍然是合法的括号序列 ...
- Codeforces Round #243 (Div. 1)——Sereja and Squares
题目链接 题意: 给n个点,求能组成的正方形的个数. 四边均平行与坐标轴 大神的分析: 经典题 我们考虑每一种x坐标,显然仅仅有<= sqrt{N}个x坐标出现了> sqrt{N}次,我们 ...
- Codeforces 599D Spongebob and Squares(数学)
D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calculati ...
- Codeforces 425A Sereja and Swaps(暴力枚举)
题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...
- codeforces 425C Sereja and Two Sequences(DP)
题意读了好久才读懂....不知道怎么翻译好~~请自便~~~ http://codeforces.com/problemset/problem/425/C 看懂之后纠结好久...不会做...仍然是看题解 ...
- codeforces B. Sereja and Stairs 解题报告
题目链接:http://codeforces.com/problemset/problem/381/B 题目意思:给定一个m个数的序列,需要从中组合出符合楼梯定义 a1 < a2 < .. ...
随机推荐
- PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD)
原文: PHP5: mysqli 插入, 查询, 更新和删除 Insert Update Delete Using mysqli (CRUD) PHP 5 及以上版本建议使用以下方式连接 MySQL ...
- c++模板
1.从 python 说起 def add(a, b): return a + b; print add(3.1, 5.1); #8.2 print add("abc", &quo ...
- edwin报警和监控平台近期的更新(python源码)
edwin从发布以来, 得到了不少关注, 获得了不少star. 最近又做了一些很有意义的改进, 同时完善了部分文档. 项目地址: https://github.com/harryliu/edwin , ...
- SQL 语句-partition by
/****** ******/ 初始化数据 create table employee (empid int ,deptid int ,salary decimal(10,2)) insert int ...
- POJ 2411 Mondriaan's Dream
状压DP Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9938 Accepted: 575 ...
- mybatis批量插入数据到oracle
mybatis 批量插入数据到oracle报 ”java.sql.SQLException: ORA-00933: SQL 命令未正确结束“ 错误解决方法 oracle批量插入使用 insert a ...
- 大数据之pig 命令
1.pig与hive的区别 pig和hive比较类似的,都是类sql的语言,底层都是依赖于hadoop 走的mapreduce任务. pig和hive的区别就是,想要实现一个业务逻辑的话, ...
- Hadoop之Storm其他_pom
1.pom 配置 <dependency> <groupId>org.apache.storm</groupId> <artifactId>storm- ...
- vue.js 简单入门
转载自:http://blog.csdn.net/violetjack0808/article/details/51451672 <!DOCTYPE html> <html lang ...
- Svn + tomcat + Hudson持续集成部署
1.首先下载hudson 2. 我这里使用hudson-3.0.1版本 3. 下载后hudson是一个 war 包 4. 操作部署: (1). 直接将hudson的war包复制到tomcat的weba ...