uva 1382 Distant Galaxy(离散化+枚举技巧)

题目:

You are observing a distant galaxy using a telescope above the Astronomy Tower, and you think that a rectangle drawn in that galaxy whose edges are parallel to coordinate axes and contain maximum star systems on its edges has a great deal to do with the mysteries of universe. However you do not have the laptop with you, thus you have written the coordinates of all star systems down on a piece of paper and decide to work out the result later. Can you finish this task?

Input

There are multiple test cases in the input file. Each test case starts with one integer N, (1 ≤ N ≤ 100), the number of star systems on the telescope.N lines follow, each line consists of two integers: the X and Y coordinates of the Kth planet system. The absolute value of any coordinate is no more than 109, and you can assume that the planets are arbitrarily distributed in the universe.

N = 0 indicates the end of input file and should not be processed by your program.

Output

For each test case, output the maximum value you have found on a single line in the format as indicated in the sample output.

题意:

给出直角坐标系的n个点,找一个矩形使得此矩形四条边上的点的数目最多,输出最大点的数量。

解题思路:

数据范围有点大,但是点只要100个,先离散化,然后枚举上下边(n^2),每次枚举算出该条件下,算出上下边加上右边覆盖的点数合,然后用其中最大的减去最小的就是一个矩形覆盖的点数。

代码:



#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
struct Node
{
        int x,y;
}p[110];
bool cmp(Node a,Node b)
{
        return a.x<b.x;
}
int y[110];
int l[110],d[110],d1[110];
int n;
int solve()
{
        int ans=0;
        sort(y+1,y+1+n);
        sort(p+1,p+1+n,cmp);
        int m=unique(y+1,y+1+n)-y-1;
        if(m<=2)
        {
                return n;
        }
        for(int a=1;a<=m;a++)
                for(int b=a+1;b<=m;b++)
                {
                        int maxy=y[b],miny=y[a],k=0;
                        memset(d,0,sizeof d);
                        memset(d1,0,sizeof d1);
                        memset(l,0,sizeof l);
                        for(int i=1;i<=n;i++)
                                {
                                        if(i==1||p[i].x!=p[i-1].x)
                                        {
                                                k++;
                                                if(k>1)
                                                        l[k]=l[k-1]+d1[k-1]-d[k-1];
                                        }
                                        if(p[i].y>=miny&&p[i].y<=maxy) d1[k]++;
                                        if(p[i].y>miny&&p[i].y<maxy) d[k]++;
                                }
                        if(k<=2)
                                return n;
                        int mmm=0;
                        for(int i=1;i<=k;i++)
                        {
                                ans=max(ans,d1[i]+l[i]+mmm);
                                mmm=max(mmm,d[i]-l[i]);
                        }

                }
        return ans;
}
int main()
{
        int w=0;
        while(cin>>n&&n)
        {
                for(int i=1;i<=n;i++)
                {
                        cin>>p[i].x>>p[i].y;
                        y[i]=p[i].y;
                }
                cout<<"Case "<<++w<<": "<<solve()<<endl;
        }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值