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:正方形要平行于坐标轴

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <string>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 using namespace std;
11 
12 #define ll long long
13 #define inf 0x3f3f3f3f
14 #define eps 1e-8
15 
16 const int maxn = 100010;
17 const int sqrtmaxn = (int)sqrt(maxn+.0) + 1;
18 vector<int>vec[maxn];
19 //set<int>st[maxn*2];
20 inline bool find(int x,int y){
21     if(x>=maxn)return false;
22     return binary_search(vec[x].begin(),vec[x].end(),y);
23 }
24 int main(){
25     int n;
26     while(~scanf("%d",&n)){
27         //for(int i=0;i<maxn;++i)vec[i].clear(),st[i].clear();
28         for(int i=0;i<n;++i){
29             int x,y;
30             scanf("%d%d",&x,&y);
31             vec[x].push_back(y);
32             //st[x].insert(y);
33         }
34         for(int i=0;i<maxn;++i)sort(vec[i].begin(),vec[i].end());
35         ll ans=0;
36         for(int x=0;x<maxn;++x){
37             if(vec[x].size()<sqrtmaxn){
38                 for(int i=0;i<vec[x].size();++i){
39                     for(int j=i+1;j<vec[x].size();++j){
40                         int d=vec[x][j]-vec[x][i];
41                         if(find(x+d,vec[x][i]) && find(x+d,vec[x][j]))++ans;
42                     }
43                 }
44             }
45             else {
46                 for(int xx=x+1;xx<maxn;++xx){
47                     for(int i=0;i<vec[xx].size();++i){
48                         int yy=vec[xx][i],d=xx-x;
49                         if(find(x,yy) && find(x,yy+d) && find(xx,yy+d))++ans;
50                     }
51                 }
52             }
53         }
54         printf("%I64d\n",ans);
55     }
56     return 0;
57 }
View Code

 

 

 

posted @ 2014-04-28 14:59  nextbin  阅读(889)  评论(0编辑  收藏  举报