首页 > 其他分享 >LAoj 3695 - Distant Galaxy (DP&几何)好题高效

LAoj 3695 - Distant Galaxy (DP&几何)好题高效

时间:2023-04-28 11:01:22浏览次数:33  
标签:int 3695 LAoj 好题 systems zz test include define


You are observing a distant galaxy using a telescope above the Astronomy Tower, and you think thata rectangle drawn in that galaxy whose edges are parallel to coordinate axes and contain maximumstar systems on its edges has a great deal to do with the mysteries of universe. However you do nothave the laptop with you, thus you have written the coordinates of all star systems down on a piece ofpaper and decide to work out the result later. Can you finish this task?InputThere 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 Xand Y coordinates of the K-th planet system. The absolute value of any coordinate is no more than109, 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.OutputFor each test case, output the maximum value you have found on a single line in the format as indicatedin 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

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 110
#define ll long long
using namespace std;
struct zz
{
	int x;
	int y;
}p[N];
int cmp(zz a,zz b)
{
	if(a.x==b.x)
		return a.y<b.y; 
	return a.x<b.x;
}
int n,m;
int y[N],on[N],on2[N],l[N];
int solve()
{
	sort(p,p+n,cmp);
	sort(y,y+n);
	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 ymi=y[a];
			int yma=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;
					l[k]=k==0?0:l[k-1]+on2[k-1]-on[k-1];
				}
				if(p[i].y>ymi&&p[i].y<yma)
					on[k]++;
				if(p[i].y>=ymi&&p[i].y<=yma)
					on2[k]++;
			}
			if(k<=2)
				return n;
			int mm=0;
			for(int j=1;j<=k;j++)
			{
				ans=max(ans,l[j]+on2[j]+mm);
				mm=max(mm,on[j]-l[j]);
			}
		}
	}
	return ans;
}
int main()
{
	int T=1;
	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",T++,solve());
	}
	return 0;
}

 

标签:int,3695,LAoj,好题,systems,zz,test,include,define
From: https://blog.51cto.com/u_16079508/6233588

相关文章