欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【寒假】uva10382 (区间覆盖)

程序员文章站 2023-12-27 10:29:09
...

问题分析:是uva10020的升级,稍微转换下就可以了,坑爹的是题目中说要输入整数,但只有定义成double型才可以通过。。。。。
转换如下图(引用)
【寒假】uva10382 (区间覆盖)
代码:

#include<iostream>
#include<algorithm>
#include<string.h>
#include<cmath>
using namespace std;
struct section
{
	double l;
	double r;
}q[100005];
//int a[100005];
int L, W;
/*void dispose(int a, int b,int &i)
{
	double r = b;
	double loc = a;
	double w = W;
	double temp = sqrt(r*r - 0.25*w*w);
	if (loc - temp >= L || loc + temp <= 0);
	else
	{
		q[i].l = loc - temp;
		q[i++].r = loc + temp;
	}
}*/
int cmp(section a, section b)
{
	return a.l < b.l;
}
int main()
{
	
	int n;
	int s;
	while (scanf("%d%d%d", &n, &L, &W) != EOF)
	{
		double ta, tb;
		int j = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> ta >> tb;
			if (tb*2 <= W)
			{
				continue;
			}
			else
			{
				q[j].l = ta - sqrt(1.0*tb*tb - 0.25*W*W);
				q[j++].r=ta+ sqrt(1.0*tb*tb - 0.25*W*W);
			}
		//	dispose(ta, tb, j);
		}
		sort(q, q + j, cmp);
		if (q[0].l > 0)
		{
			cout << -1 << endl;
			continue;
		}
		
		
			double nr = 0;
			double nl = 0;
			s = 0;
			while (nr < L)
			{
				int flag = 0;
				//int number;
				//nr = nl;
				for (int i = 0; i < j; i++)
				{
					if (nr < q[i].r&&q[i].l <= nl)
					{
						nr = q[i].r;
						//number = i;
						flag = 1;
					}
				}
				if (!flag)
				{
					s = -1;
					break;
				}
				//a[s] = number;
				s++;
				nl = nr;

			}
		cout << s << endl;
		//memset(q, 0, sizeof(q));
	
	}

}

上一篇:

下一篇: