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?
![](https://i-blog.csdnimg.cn/blog_migrate/bc9cd74b7b1829e5e95a62c4cf1962f7.png)
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;
}