seaicelin的博客

坚持吧,少年!

1328
#include<iostream>
#include<cmath>

using namespace std;

const int MAX = 1001;
const int X = 0;
const int Y = 1;

int N = 0;
int D = 0;

double getX(int d, int y);
void quick_sort(double array[MAX][2], int l, int r);

int main()
{

	freopen("input.txt", "r", stdin);
	setbuf(stdout, NULL);

	int test_case = 0;
	bool over = false;
	int answer = 0;

	while(true)
	{

		cin >> N >> D;
		if(N == 0 && D == 0)
		{
			break;
		}

		double Radius[MAX][2] = {0};

		test_case++;
		over = false;
		answer = 1;

		int x, y;
		for(int i = 0; i < N; i++)
		{
			cin >> x >> y;
			if(y > D)
			{
				over = true;
			}
			if(over != true)
			{
				double z = getX(D, y);
				Radius[i][X] = x - z;
				Radius[i][Y] = x + z;
			}
		}
		
		if(over == true)
		{
			cout << "Case " << test_case << ": " << -1 <<endl;
			continue;
		}
	
		quick_sort(Radius, 0, N - 1);

		double right = Radius[0][Y];

		for(int i = 1; i < N; i++)
		{
			if(Radius[i][X] > right)
			{
				answer++;
				right = Radius[i][Y];
			}
			else if(Radius[i][Y] < right)
			{
				right = Radius[i][Y];
			}
		}
		cout << "Case " << test_case << ": " << answer <<endl;
	}

	return 0;
}

double getX(int d, int y)
{
	return sqrt((double)(d * d * 1.0 - y * y * 1.0));
}
void quick_sort(double array[MAX][2], int l, int r)
{
	if(l < r)
	{
		int i = l;
		int j = r;
		double x = array[l][X];
		double y = array[l][Y];
		while(i < j)
		{
			while(i < j && array[j][X] > x)
				j--;
			if(i < j)
			{
				array[i][X] = array[j][X];
				array[i][Y] = array[j][Y];
				i++;
			}
			while(i < j && array[i][X] < x)
				i++;
			if(i < j)
			{
				array[j][X] = array[i][X];
				array[j][Y] = array[i][Y];
				j--;
			}
		}
		array[i][X] = x;
		array[i][Y] = y;
	
		quick_sort(array, l, i - 1);
		quick_sort(array, i + 1, r);
	}
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/amd123456789/article/details/46793801
文章标签: POJ ACM
个人分类: 算法入门学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

1328

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭