ACM 算法艺术与信息学竞赛 1.2.1 图书馆

这个题在POJ和URAL上都可以找到。下面给出链接

ural-1188


PKU-1116


下面还是把题目粘一下

Library
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 602 Accepted: 170

Description

Castaway Robinson Crusoe is living alone on a remote island. One day a ship carrying a royal library has wrecked nearby. Usually Robinson brings any useful stuff from the shipwreck to his island, and this time he has brought a big chest with books.

Robinson has decided to build a bookcase for these books to create his own library. He cut a rectangular niche in the rock for that purpose, hammered in wooden pegs, and placed wooden planks on every pair of pegs that have the same height, so that all planks are situated horizontally and suit to act as shelves.

Unfortunately, Robinson has discovered that one especially old and big tome does not fit in his bookcase. He measured the height and width of this tome and has decided to redesign his bookcase in such a way, as to completely fit the tome on one of the shelves, taking into account locations of other shelves and the dimensions of the niche. With each shelf in the bookcase, one of the following operations should be made:

1. Leave the shelf on its original place.

2. Move the shelf to the left or to the right.

3. Shorten the shelf by cutting off a part of the plank and optionally move it to the left or to the right.

4. Move one of the pegs to a different place at the same height and move the shelf to the left or to the right.

5. Shorten the shelf by cutting off a part of the plank, move one of the pegs to a different place at the same height, and optionally move the shortened shelf to the left or to the right.

6. Remove the shelf from the bookcase along with both supporting pegs.

We say that the shelf is properly supported by its pegs, if exactly two distinct pegs support the shelf and the center of the shelf is between its pegs or coincides with one of the pegs. The original design of Robinson's library has all the shelves properly supported by their pegs and lengths of all shelves are integer number of inches. The Robinson may only cut an integer number of inches from the planks, because he has no tools for more precise measurements. All remaining shelves after the redesign must be properly supported by their pegs.

You are to find the way to redesign Robinson's library to fit the special old tome without changing original design too much. You have to minimize the number of pegs that are to be removed from their original places during the redesign (operations 4 and 5 remove one peg, and operation 6 removes two pegs). If there are different ways to solve the problem, then you are to find the one that minimizes the total length of planks that are to be cut off (operations 3 and 5 involve cutting something from the planks, and operation 6 counts as if cutting off the whole plank). Width of planks and diameter of pegs shall be considered zero.

The tome may not be rotated. The tome should completely (to all its width) stand on one of the shelves and may only touch other shelves, their pegs or niche's edge.

Input

The first line of the input file contains four integer numbers XN, YN, XT, and YT, separated by spaces. They are, correspondingly, width and height of the niche, and width and height of the old tome in inches (1 <= XN, YN, XT, YT <= 1000).

The second line of the input file contains a single integer number N (1 <= N <= 100) that represents the number of the shelves. Then N lines follow. Each line represents a single shelf along with its two supporting pegs, and contains five integer numbers yi, xi, li, x1i, x2i, separated by spaces, where:

?yi (0 < yi < YN) - the height of the ith shelf above the bottom of the niche in inches.
?xi (0 <= xi < XN) - the distance between the left end of the ith shelf and the left edge of the niche in inches.
?li (0 < li <= XN - xi) - the length of the ith shelf in inches.
?x1i (0 <= x1i <= li/2) - the distance between the left end of the ith shelf and its leftmost supporting peg in inches.
?x2i (li/2 <= x2i <= li; x1i < x2i) - the distance between the left end of the ith shelf and its rightmost supporting peg in inches.

All shelves are situated on different heights and are properly supported by their pegs. The problem is guaranteed to have a solution for the input data.

Output

The output file shall contain two integer numbers separated by a space. The first one is the minimal number of pegs that are to be removed by Robinson from their original locations to place the tome. The second one is the minimal total length of planks in inches that are to be cut off during the redesign that removes the least number of pegs.

Sample Input

11 8 4 6
4
1 1 7 1 4
4 3 7 1 6
7 2 6 3 4
2 0 3 0 3

Sample Output

1 3

Source


下面给出大牛们的代码。留做备份

#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
struct shelf
{
	int x, y, len, x1, x2;
}a[101];

int n, cx, cy, tx, ty;
int minpeg = 20000, mincost = 1000000;

bool cmp(const shelf &a, const shelf &b)
{
	return a.y < b.y;
}

void check(int k, int x, int &peg, int &cost)
{
	for (int i = k + 1; i < n; i++)
	{
		if (a[i].y >= a[k].y + ty) break;
		if (a[i].x + a[i].len <= x || a[i].x >= x + tx) continue;
		if (a[i].x2 <= x)
		{
			int rest;
			if (x - a[i].x1 <= a[i].x1) rest = 2 * (x - a[i].x1); else rest = x;
			if (rest < a[i].len) cost += a[i].len - rest;
		}
		else if (a[i].x1 >= x + tx)
		{
			int rest;
			if (a[i].x2 - x - tx <= cx - a[i].x2) rest = 2 * (a[i].x2 - x - tx);
			else rest = cx - x - tx;
			if (rest < a[i].len) cost += a[i].len - rest;
		}
		else if (a[i].x1 <= x && a[i].x2 > x && a[i].x2 < x + tx)
		{
			if (x == 0) peg += 2, cost += a[i].len;
			else
			{
				peg++;
				if (a[i].len > x) cost += a[i].len - x;
			}
		}
		else if(a[i].x1 > x && a[i].x1 < x + tx && a[i].x2 >= x + tx)
		{
			if (x + tx == cx) peg += 2, cost += a[i].len;
			else
			{
				peg++;
				if (a[i].len > cx - x - tx) cost += a[i].len - cx + x + tx;
			}
		}
		else if (a[i].x1 <= x && a[i].x2 >= x + tx)
		{
			if (x == 0 && tx == cx) peg += 2; else peg++;
			int rest = max(x, cx - x - tx);
			if (a[i].len > rest) cost += a[i].len - rest;
		}
		else if (a[i].x1 > x && a[i].x2 < x + tx)
		{
			peg += 2;
			cost += a[i].len;
		}
	}
}

void work(int k)
{
	for (int i = 0; i + tx <= cx; i++)
	{
		int peg = 0, cost = 0;
		if (i + a[k].len < a[k].x1) continue;
		if (a[k].x2 + a[k].len < i + tx) break;
		if (2 * (a[k].x1 - i) > a[k].len || 2 * (i + tx - a[k].x2) > a[k].len
			|| a[k].x2 - i > a[k].len || i + tx - a[k].x1 > a[k].len) peg++;

		check(k, i, peg, cost);

		if (peg < minpeg || (peg == minpeg && cost < mincost))
		{
			minpeg = peg;  mincost = cost;
		}
	}
}

int main()
{
	cin>>cx>>cy>>tx>>ty;
	cin>>n;

	for (int i = 0; i < n; i++)
	{
		cin>>a[i].y>>a[i].x>>a[i].len>>a[i].x1>>a[i].x2;
		a[i].x1 += a[i].x;
		a[i].x2 += a[i].x;
	}
	sort(a, a + n, cmp);

	for (int i = 0; i < n; i++)
	{
		if (a[i].y + ty > cy) break;
		if (a[i].len >= tx) work(i);
	}

	cout<<minpeg<<" "<<mincost;
	return 0;
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值