uva 10574 - Counting Rectangles (离散化)

Problem H

Counting Rectangles

Input: Standard Input

Output: Standard Output

Time Limit: 3 Seconds

 

Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular if and only if its sides are all parallel to the axis.

 

Input

The first line contains the number of tests t(1<=t<=10). Each case contains a single line with a positive integern(1<=n<=5000), the number of points. There are n lines follow, each line contains 2 integers x(0<=xy<=109) indicating the coordinates of a point.

 

Output

For each test case, print the case number and a single integer, the number of regular rectangles found.

 

Sample Input                             Output for Sample Input

2

5

0 0

2 0

0 2

2 2

1 1

3

0 0

0 30

0 900

Case 1: 1

Case 2: 0


Problemsetter: Rujia Liu, Member of Elite Problemsetters' Panel

Special Thanks: IOI2003 China National Training Team

直接用map判断(n^2logn)超时了,改成先离散化再判断(n^2)可AC。

#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<cmath>
using namespace std;
const int maxn = 5000 + 5;
const int INF = 1000000000;
const double eps = 1e-6;
typedef long long LL;
typedef pair<int, int> P;

P p[maxn];
int vis[maxn][maxn];
vector<int> vx, vy;

int main(){
    int t, kase = 0;
    scanf("%d", &t);
    while(t--){
        kase++;
        int n;
        scanf("%d", &n);
        for(int i = 0;i < n;i++)
            scanf("%d%d", &p[i].first, &p[i].second);
        vx.clear();vy.clear();
        for(int i = 0;i < n;i++){
            vx.push_back(p[i].first);
            vy.push_back(p[i].second);
        }
        sort(vx.begin(), vx.end());
        sort(vy.begin(), vy.end());
        vx.erase(unique(vx.begin(), vx.end()), vx.end());
        vy.erase(unique(vy.begin(), vy.end()), vy.end());
        memset(vis, 0, sizeof vis);
        for(int i = 0;i < n;i++){
            int x = find(vx.begin(), vx.end(), p[i].first)-vx.begin();
            int y = find(vy.begin(), vy.end(), p[i].second)-vy.begin();
            p[i].first = x;
            p[i].second = y;
            vis[x][y] = 1;
        }
        int ans = 0;
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                int x0 = p[i].first;
                int y0 = p[i].second;
                int x1 = p[j].first;
                int y1 = p[j].second;
                if(x1!=x0 && y1!=y0 && vis[x1][y0] && vis[x0][y1])
                    ans++;
            }
        }
        printf("Case %d: %d\n", kase, ans/4);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值