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

UVA - 10043 Chainsaw Massacre

时间:2014-08-09 18:53:58      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   strong   

Description

bubuko.com,布布扣


 Problem E: Chainsaw Massacre 

Background 

As every year the Canadian Lumberjack Society has just held its annual woodcutting competition and the national forests between Montreal and Vancouver are devastated. Now for the social part!In order to lay out an adequate dance floor for the evening partythe organizing committee is looking for a large rectangular area without trees. Naturally, all lumberjacks are already drunk and nobody wants to take the risk of having any of them operate a chainsaw.

The Problem 

The organizing committee has asked you to find the largest yet freerectangle which could serve as the dance floor. The area inwhich you should search is also rectangular and the dance floor mustbe entirely located in that area.Its sides should be parallel to the borders of the area.It is allowed that the dance floor is located at the borders of the areaand also that trees grow on the borders of the dance floor.What is the maximum size of the dance floor?

The Input 

The first line of the input specifies the number of scenarios. For each scenario, the first line provides the length l and widthw of the area in meters (bubuko.com,布布扣,both integers). Each ofthe following lines describes either a single tree, or a line of treesaccording to one of the following formats:

  • 1 x y, where the ``one‘‘ characterizes a single tree, and x and y provide its coordinates in meters with respect to the upper leftcorner.
  • k x y dx dy, where k>1 provides the number of trees in a line withcoordinates bubuko.com,布布扣.
  • 0 denotes the end of the scenario.
The coordinates x, y, dx, and dy are given as integers. It is guaranteed that all the trees are situated in the area, i.e. have coordinatesin bubuko.com,布布扣.There will be at most 1000 trees.

The Output 

For each scenario print a line containing the maximum size of the dance floor measured in square meters.

Sample Input 

2
2 3
0
10 10
2 1 1 8 0
2 1 9 8 0
0

Sample Output 

6
80

题意:平面上有n棵树,找出一个内部没有树的,面积最大的矩形

思路:以y坐标排序然后扫描,每次先扫到一棵树就可以知道它与上一棵树之间的距离,然后更新统计每个x坐标的最左边和最右边,每次都计算一次

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
const int maxn = 10010;

int h[maxn], l[maxn], r[maxn];
int n, m, ans;
map<int, vector<int> > tree;

void check() {
	for (int i = 0, j = n; i <= n; i++, j--) {
		for (l[i] = i; l[i] > 0 && h[l[i]-1] >= h[i]; )
			l[i] = l[l[i]-1];
		for (r[j] = j; r[j] < n && h[r[j]+1] >= h[j]; )
			r[j] = r[r[j]+1];
	}
}

void cal() {
	for (int i = 0; i <= n; i++) {
		int tmp = h[i] * (r[i] - l[i] + 2);
		ans = max(ans, tmp);
	}
}

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		tree.clear();
		scanf("%d%d", &n, &m);
		int op, x, y, dx, dy;
		while (1) {
			scanf("%d", &op);
			if (op == 0) 
				break;
			else if (op == 1) {
				scanf("%d%d", &x, &y);
				tree[y].push_back(x);
			}
			else {
				scanf("%d%d%d%d", &x, &y, &dx, &dy);
				for (int i = 0; i < op; i++) {
					tree[y].push_back(x);
					y += dy, x += dx;
				}
			}
		}
		tree[m];
		ans = max(n, m);
		int last = 0;
		memset(h, 0, sizeof(h));
		map<int, vector<int> >::iterator it;
		for (it = tree.begin(); it != tree.end(); it++) {
			int d = it->first - last;
			last += d;
			for (int i = 1; i < n; i++)
				h[i] += d;
			check();
			cal();
			vector<int> tmp = it->second;
			for (int i = 0; i < tmp.size(); i++)
				h[tmp[i]] = 0;
		}
		printf("%d\n", ans);
	}
	return 0;
}



UVA - 10043 Chainsaw Massacre,布布扣,bubuko.com

UVA - 10043 Chainsaw Massacre

标签:des   style   blog   http   color   os   io   strong   

原文地址:http://blog.csdn.net/u011345136/article/details/38457259

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