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

来源:互联网 发布:linux刻录光盘 编辑:程序博客网 时间:2024/05/17 22:36

题目:

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
原创粉丝点击