码迷,mamicode.com
首页 > 其他好文 > 详细

uvalive 4851

时间:2015-03-28 21:51:17      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:uvalive

题意:有n个餐馆,其中两个餐馆a,b还是宾馆(纵坐标相等),在m*m的矩阵上建餐厅p,如果位置好,对于已存在的餐厅q(包括a和b),就会有dist(p,a) < dist(q,a) || dist(p,b) < dist(q,b) (曼哈顿距离),问矩阵上有多少个好的位置。

题解:直接暴力肯定超时,可以考虑一列一列的考虑,让A横坐标更小,从A的横坐标开始,到B的横坐标内所有位置有可能是好位置,然后计算出每列上已有餐厅到A的纵坐标最小距离存到d[i],然后从左到右更新一遍(每向右移动一次,最小距离就和前一列最小距离加1和当前列最小距离的较小值),然后从右到左更新一遍,最后再和边界值y1上下方比较一次,得到最终解。。


#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = 60005;

int main() {
	int t, n, m, x1, x2, y1, y2, d[N];
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &m, &n);
		scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
		if (x1 > x2)
			swap(x1, x2);
		for (int i = x1; i <= x2; i++)
			d[i] = m;
		n -= 2;
		int x, y;
		for (int i = 0; i < n; i++) {
			scanf("%d%d", &x, &y);
			d[x] = min(d[x], abs(y - y1));//当前列到A、B可能的好位置的最大值
		}
		d[x1] = 0;
		for (int i = x1 + 1; i < x2; i++)
			d[i] = min(d[i], d[i - 1] + 1);//对于到B,前一列到后一列可能的好位置数量加1然后和当前列比较
		d[x2] = 0;
		for (int i = x2 - 1; i > x1; i--)
			d[i] = min(d[i], d[i + 1] + 1);
		long long res = 0;
		for (int i = x1 + 1; i < x2; i++)
			if (d[i]) {
				res++;
				res += min(d[i] - 1, y1);//y1下方的限制
				res += min(d[i] - 1, m - 1 - y1);//y1上方的限制
			}
		printf("%lld\n", res);
	}
	return 0;
}

uvalive 4851

标签:uvalive

原文地址:http://blog.csdn.net/hyczms/article/details/44706097

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!