Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴代码,求大神指教

Posted by 20152430226 at 2017-03-26 20:28:19 on Problem 1328
// 贪心 
// 找出每个岛的雷达区间,以左点从小到大排序。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

const int MAXLAND = 1000;
const float INF = 1999999999.9;
struct Radar
{
	float lo, hi;
}radar[MAXLAND+24];

bool _cmp(Radar a, Radar b)
{
	if (a.lo != b.lo)
		return a.lo < b.lo;
	else
		return a.hi < b.hi;
}
int main()
{
	int n;
	int d;
	int i;
	int x, y;
	int cir = 0;
	int count = 0;
	float temp;
	float flag;				// 右最小值 
	bool mark;
	
	while (scanf("%d%d",&n, &d) == 2 && n != 0)
	{
		// initialize
		++cir;
		mark = true;
		count = 0;
		flag = radar[0].hi = -INF;
		for (i = 1; i <= n; i++)
		{
			scanf("%d%d", &x, &y); 	
			if (y > d)
				mark = false;
			if (mark)
			{
				temp =  sqrt((double)(d * d - y * y));
				radar[i].lo = x - temp;
				radar[i].hi = x + temp; 
			}
		}
		// working hard
		if (!mark)
		{
			printf("Case %d: -1\n",cir);
			continue;
		}
		else
		{	
			sort(radar+1, radar+n+1,_cmp);
			for (i = 1; i <= n; i++)
			{
				if (radar[i].lo > flag)
				{
					count++;
					flag = radar[i].hi;
				}
				else
				{
					if (flag > radar[i].hi)
						flag = radar[i].hi;
				}
			}
			printf("Case %d: %d\n", cir, count);
		}	
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator