poj1328

转化成线段后,从左到右贪心即可。

这题最坑的是我输入的时候如果发现d<0你是不能break的或者出现非法输入,因为其他输入还在流中所以你要继续,看我的break就被注释掉了,

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const double eps = 1e-8;
struct nodee
{
	int num;
	double v;
	int kind;
};
bool comm(nodee a, nodee b)
{
	if (fabs(a.v - b.v)<eps)
		return a.kind < b.kind;
	return a.v < b.v;
}
int n,tot;
double d;
nodee node[2020];
bool visit[2020];
queue<nodee>que;
int main()
{
	int k = 1;
	while (scanf("%d%lf", &n, &d) != EOF && (n || d))
	{
		tot = 0; bool judge = false;
		while (!que.empty())
			que.pop();
		for (int i = 0; i < n; i++)
			visit[i] = 0;
		for (int i = 0; i < n; i++)
		{
			double a, b;
			scanf("%lf%lf", &a, &b);
			if (b > d)
			{
				judge = true;
			//	break;
			}
			node[tot].v = a - (sqrt(d*d - b*b)); node[tot].num = i; node[tot].kind = 0; tot++;
			node[tot].v = a + (sqrt(d*d - b*b)); node[tot].num = i; node[tot].kind = 1; tot++;
		}
		if (judge||d<0)
		{
			printf("Case %d: -1\n", k++);
			continue;
		}
		sort(node, node + tot, comm);
		int ans = 0;
		for (int i = 0; i < tot; i++)
		{
			if (node[i].kind == 1 )
			{
				if (visit[node[i].num] == 0)
				{
					ans++;
					while (!que.empty())
					{
						nodee cc = que.front();
						visit[cc.num] = 1;
						que.pop();
					}
				}
				else
					continue;
			}
			else
			{
				que.push(node[i]);
			}
		}
		printf("Case %d: %d\n", k++,ans);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值