UVa 1042 Lots of Sunlight (枚举&最优斜率)

1042 - Lots of Sunlight

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3483


思路:对于给定的楼层,枚举左、右的最大斜率。

atan(k)/pi就表示所占时间的百分比,注意计算结束时间时要用pi-atan(k)处理下。

提交后居然返回了第一名。。


完整代码:

/*0.118s*/

#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int BEGIN = (5 * 60 + 37) * 60, END = (18 * 60 + 17) * 60; /// 秒

int m[105], d[105], pos[105];

int main()
{
	int n, cas = 0, id, Floor, W, H, i, beg, end;
	double left, right;
	while (scanf("%d", &n), n)
	{
		scanf("%d%d", &W, &H);
		for (i = 1; i < n; ++i)
		{
			scanf("%d%d", &m[i], &d[i]);
			pos[i + 1] = pos[i] + W + d[i];
		}
		scanf("%d", &m[n]);
		printf("Apartment Complex: %d\n", ++cas);
		///
		while (scanf("%d", &Floor), Floor)
		{
			id = Floor % 100, Floor /= 100, left = right = 0.0;
			if (id < 1 || id > n || Floor < 1 || Floor > m[id])
			{
				printf("Apartment %d: Does not exist\n", Floor * 100 + id);
				continue;
			}
			for (i = 1; i < id; ++i)
				if (m[i] >= Floor)
					left = max(left, (double)((m[i] - Floor + 1) * H) / (pos[id] - pos[i] - W));/// 高度差/距离
			for (i = id + 1; i <= n; ++i)
				if (m[i] >= Floor)
					right = max(right, (double)((m[i] - Floor + 1) * H) / (pos[i] - pos[id] - W));
			beg = (int)(atan(left) * (END - BEGIN) / M_PI + eps) + BEGIN; ///注意+eps!
			end = (int)((M_PI - atan(right)) * (END - BEGIN) / M_PI + eps) + BEGIN;
			printf("Apartment %d: %02d:%02d:%02d - %02d:%02d:%02d\n", Floor * 100 + id,
				   beg / 3600, beg / 60 % 60, beg % 60,
				   end / 3600, end / 60 % 60, end % 60);
		}
	}
	return 0;
}


你可能感兴趣的:(C++,ACM,uva)