BZOJ3543: [ONTAK2010]Garden

http://www.lydsy.com/JudgeOnline/problem.php?id=3543

  首先明确两点,对于一个平行于坐标轴的正方形,只需要确定的一条边就可以,比如说下边或者右边。还有一个结论:n个点的二维点集的平行坐标轴的正方形个数是O(n√n)的。

  所以我们大概就明白了这题是需要暴力统计的。

  将横坐标相同的点放进同一个集合,点数>√n的称为大集合,否则是小集合。将读入的点丢进hash里。

  枚举每个集合:

    对于大集合,易知不超过√n个,每个就枚举其左侧的每个点,那么就确定了下边界,hash判定一下另外三个点的存在性即可。复杂度O(n√n)。

    对于小集合,暴力枚举集合内的点对来确定右边,hash判定。总复杂度不超过O(n√n)。

#include<bits/stdc++.h>
#include<tr1/unordered_set>
using namespace std;
const int maxn=100010,maxy=2000010;
struct point{
    int x,y;
    point(): x(0),y(0) { }
    point(int x_,int y_): x(x_),y(y_) { }
    bool operator==(const point &b)const{
        return x==b.x && y==b.y;
    }
}a[maxn];
struct hasher{
    long long operator()(const point &t)const{
        const long long seed=23333;
        return t.x*seed+t.y;
    }
};
std::tr1::unordered_set<point,hasher> t;
vector<int> heavy;
int l[maxy],r[maxy]; bool flag[maxy];
int n,S;

inline bool cmp_y(const point &a,const point &b){
    return a.y<b.y || (a.y==b.y && a.x<b.x);
}
void init(){
    scanf("%d",&n),S=sqrt(n)*2;
    t.clear(),heavy.clear();
    for(int i=1;i<=n;++i){
        scanf("%d%d",&a[i].x,&a[i].y);
        a[i].y+=1e6,t.insert(a[i]);
    }
    sort(a+1,a+n+1,cmp_y);
}

void solve(){
    memset(l,-1,sizeof(l));
    memset(r,-1,sizeof(r));
    memset(flag,false,sizeof(flag));
    for(int u=1,v=1;u<=n;u=v){
        while(v<=n && a[v].y==a[u].y) ++v;
        l[a[u].y]=u,r[a[u].y]=v;
        if(v-u>S) heavy.push_back(a[u].y),flag[a[u].y]=true;
    }
    int ans=0;
    for(int i=1,x,y,d;i<=n;++i){
        x=a[i].x,y=a[i].y;
        if(flag[y]){
            for(int j=0;j<heavy.size();++j){
                d=heavy[j]-y; if(d<=0) continue;
                if(t.count(point(x,y+d)) && t.count(point(x+d,y)) && t.count(point(x+d,y+d)))
                    ++ans;
            }
        }
        else{
            for(int j=i+1;j<r[y];++j){
                d=a[j].x-x; if(!d) continue;
                if(t.count(point(x,y+d)) && t.count(point(x+d,y+d)))
                    ++ans;
                if(y-d>=0 && flag[y-d] && t.count(point(x,y-d)) && t.count(point(x+d,y-d)))
                    ++ans;
            }
        }
    }
    printf("%d\n",ans);
}

int main(){
    init();
    solve();
    return 0;
}

my code

时间: 2024-10-05 18:41:29

BZOJ3543: [ONTAK2010]Garden的相关文章

bzoj千题计划155:bzoj3543: [ONTAK2010]Garden

http://www.lydsy.com/JudgeOnline/problem.php?id=3543 枚举每一个点,作为左下角 然后枚举 相同的x坐标,y坐标 少的那个 作为另一个角 二分判断另外两个角是否存在 #include<map> #include<vector> #include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N

Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)

题目链接:http://codeforces.com/problemset/problem/459/A A. Pashmak and Garden time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Pashmak has fallen in love with an attractive girl called Parmida s

bnu 34982 Beautiful Garden(暴力)

题目链接:bnu 34982 Beautiful Garden 题目大意:给定一个长度为n的序列,问说最少移动多少点,使得序列成等差序列,点的位置能够为小数. 解题思路:算是纯暴力吧.枚举等差的起始和中间一点,由于要求定中间一点的位置.所以这一步是o(n3);然后用o(n)的算法确定说须要移动几个来保证序列等差. #include <cstdio> #include <cstring> #include <vector> #include <algorithm&g

bzoj3545 [ONTAK2010]Peaks

3545: [ONTAK2010]Peaks Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 572  Solved: 164[Submit][Status] Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问,每组询问询问从点v开始只经过困难值小于等于x的路径所能到达的山峰中第k高的山峰,如果无解输出-1. I

北京邀请赛 B. Beautiful Garden

题意:给你坐标和n个点,求最少移动的点使得n个点成等差数列 思路:既然要成等差数列,那么最起码有两个点是不动的,然后枚举这两个点中间的点的个数,最近水的要死,看了队友的代码做的 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <cmath> using namespace

hdu 5977 Garden of Eden

Garden of Eden Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 581    Accepted Submission(s): 164 Problem Description When God made the first man, he put him on a beautiful garden, the Garden

2014 SummerTrain Beautiful Garden

There are n trees planted in lxhgww's garden. You can assume that these trees are planted along the X-axis, and the coordinate of ith tree is xi. But in recent days, lxhgww wants to move some of the trees to make them look more beautiful. lxhgww will

bnu 34982 Beautiful Garden

Beautiful Garden There are n trees planted in lxhgww's garden. You can assume that these trees are planted along the X-axis, and the coordinate of ith tree is xi. But in recent days, lxhgww wants to move some of the trees to make them look more beaut

nginx+iis、NLB、Web Farm、Web Garden

nginx+iis实现负载均衡 在win2008R2上使用(NLB)网络负载均衡 NLB网路负载均衡管理器详解 [译文]Web Farm和Web Garden的区别?