UVALive - 3695:Distant Galaxy

Distant Galaxy

来源:UVALive

标签:

参考资料:《算法竞赛入门经典——训练指南》P52

相似题目:https://blog.csdn.net/wingrez/article/details/82426599

题目

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?

输入

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 K-th 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.

输出

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

输入样例

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

输出样例

Case 1: 7

参考代码

#include
#include
#define MAXN 105
using namespace std;

struct Point{
	int x,y;
	bool operator < (const Point& rhs) const{
		return x<rhs.x; 
	}
}P[MAXN];

int n;
int y[MAXN];
int on[MAXN],on2[MAXN],left[MAXN];

int solve(){
	sort(P,P+n);
	sort(y,y+n);
	int m=unique(y,y+n)-y;
	if(m<=2) return n;
	
	int ans=0;
	for(int a=0;a<m;a++){
		for(int b=a+1;b<m;b++){
			int ymin=y[a], ymax=y[b];
			int k=0;
			for(int i=0;i<n;i++){
				if(i==0 || P[i].x!=P[i-1].x){
					k++;
					on[k]=on2[k]=0;
					left[k]= k==0?0:left[k-1]+on2[k-1]-on[k-1];
				}
				if(P[i].y>ymin && P[i].y<ymax) on[k]++;
				if(P[i].y>=ymin && P[i].y<=ymax) on2[k]++;
			}
			if(k<=2) return n;
			
			int M=0;
			for(int j=1;j<=k;j++){
				ans=max(ans,left[j]+on2[j]+M);
				M=max(M,on[j]-left[j]);
			}
		}
	}
	return ans;
}

int main(){
	int kase=0;
	while(scanf("%d",&n) && n){
		for(int i=0;i<n;i++){
			scanf("%d%d",&P[i].x,&P[i].y);
			y[i]=P[i].y;
		}
		printf("Case %d: %d\n",++kase,solve());
	}
	return 0;
}

你可能感兴趣的:(【记录】算法题解)