HOJ Grandpa's Other Estate——(离散化思想的复习)

Grandpa's Other Estate

My Tags  (Edit)
 Source : ACM ICPC Tehran Regional Contest 2002
 Time limit : 5 sec Memory limit : 32 M

Submitted : 525, Accepted : 235

From our previous contest, we know that Kamran the Believer inherited many of his grandpa's belongings. Apparently, his grandpa had been a mathematician in his life with interests in puzzle solving, since he has made Kamran solve another programming problem!

Grandpa had a big garden with many valuable walnut trees. He has written in his will that Kamran can inherit one piece of square shaped land of a given size in the garden, such that its sides be parallel to the x and y axes. Taking advantage of the fact that no other restrictions have been mentioned in the will, Kamran wants to choose the land in which the most number of trees lie. Kamran is too wealthy now and thus too lazy to spend time and solve another algorithmic problem. He has hired you to solve this problem for him.

You are given the location of all trees in the big garden and the size of the land to choose. You are to write a program to find out where to choose the land so that the most number of trees lie in it. You may consider trees as points in the plane and the land as a square. You are to find the position of the square such that it includes as many points as possible. Note that the points on the border of the square are considered to be inside it.

Input
The first line of the input file contains a single integer t (1<=t<=10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1<=n<=100), the number of trees, and an integer r (1<=r<=1000), the length of the land's side, followed by n lines, each containing two integers x and y (0<=x , y <= 100,000) representing the coordinates of a walnut tree. Note that all coordinates are pairwise distinct.

Output
There should be one line per test case containing the maximum number of trees that Kamran can own.

Sample Input

1
3 1
1 2
2 1
4 3
Sample Output
2

回首一望很多以前的难题都成了水题:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 101;

/*
 * 和多校联合的那一题一个思想,就是把几何的
 * 连续进行离散化,如何离散化呢?实际上是很
 * 简单的,我们先假设找到最优解,通过平移旋
 * 转变换来实现最优解到一个可枚举的等价。
 * 假如一个边长为r正方形覆盖的点最多,那么
 * 向下平移到不能平移,和右平移到不能平移为
 * 止,那么有两个点确定了这个最优的四边形。
 * 既可以通过枚举任意两个点来实现求出最优解!
 */
struct p {
    int x, y;

    int read() {
        return scanf("%d%d", &x, &y);
    }
} pp[MAX];

bool judge(int x1, int x2, int y1, int y2, p a) {
    if ((long long) (a.x - x1)*(a.x - x2) <= 0 && (long long) (a.y - y1)*(a.y - y2) <= 0) {
        return true;
    }
    return false;
}

int main() {
    int t, n, r;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n,&r);
        for (int i = 0; i < n; i++) {
            pp[i].read();
        }
        int res = 1; //防止一个点时出现问题!
        int temp_ans;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (abs(pp[i].x - pp[j].x) > r || abs(pp[i].y - pp[j].y) > r)continue;
                int temx = min(pp[i].x, pp[j].x);
                int temy = max(pp[i].y, pp[j].y);
                temp_ans = 0;
                for (int k = 0; k < n; k++) {
                    if (judge(temx, temx + r, temy, temy - r, pp[k])) {
                        temp_ans++;
                    }
                }
                res = max(res, temp_ans);
            }
        }
        printf("%d\n", res);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值