poj 3141Distant Galaxy

Distant Galaxy
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1845 Accepted: 582

Description

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.

Sample Input

10
2 3
9 2
7 4
3 4
5 7
1 5
10 4
10 6
11 4
4 6
0

Sample Output

Case 1: 7

Source

 

 

 

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;
const int MAX = 100+5;

struct P
{
     int x,y;

     bool operator <(const P& point) const
     {
            return x < point.x;
     }

}points[MAX];



int caculate(int n)
{
    int ys[MAX];
    sort(points, points + n);
    for(int i = 0; i < n; i++)ys[i] = points[i].y;
    
    //计算y行数 
    sort(ys, ys + n);
    int y_count = unique(ys, ys + n) - ys;
    
   // for(int i = 0; i < y_count; i++)cout <<" "<<ys[i];
    if(y_count < 2)return n;
    
    int left[MAX],vertical[MAX]; 
    
   // memset(vertical, n, 0);
    int ans = 0;
    for(int up = 0; up < y_count; up++)
        for(int dwn = up + 1; dwn < y_count; dwn++)
        {
           // int up_axis = y[up],dwn_axis = y[dwn];
            
            int col = 0;
            left[0] = 0,vertical[0] = 0;
            for(int i = 0; i < n; i++)
            {
                if(i != 0 && points[i].x != points[i - 1].x)
                {
                     vertical[++col] = 0;
                     left[col] =  left[col - 1]; 
                }
                 
                if(points[i].y == ys[up] || points[i].y == ys[dwn])left[col]++;
                if(points[i].y > ys[up] && points[i].y < ys[dwn])vertical[col]++;
                
            }
            
            if(!col)return n;
            int m = vertical[0];
            
            //cout << ys[up]<<"---"<<ys[dwn]<<endl;
           // cout<<"left-"<<0<<"=>"<<left[0]<<" verti-"<<0<<"=>"<<vertical[0]<<endl; 
            for(int i = 1; i <= col; i++)
            {
                    
               // cout<<"left-"<<i<<"=>"<<left[i]<<" verti-"<<i<<"=>"<<vertical[i]<<endl; 
                ans = max(ans, left[i] + vertical[i] + m);
                m = max(m, vertical[i] - left[i - 1]);
                
            }
            
        }
    return ans;
}

int main()
{
    
    int  t,n,x,y,kase = 1;
    //scanf("%d",&t);

    while(1)
    {
        scanf("%d",&n);
        if(!n)break;

        for(int i = 0; i < n; i++)
            scanf("%d%d",&points[i].x, &points[i].y);
        
        int ans = caculate(n);   
        cout <<"Case "<<kase++<<": "<< ans<<endl;
    }

    //system("pause");
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值