Farmer John has built a sand castle

文章详细阐述了如何通过调整城堡的防御工事高度,利用不同的工匠成本计算最优修改方案,以最小化整体改造成本。
时限:1000ms 内存限制:10000K  总时限:3000ms

描述:

Farmer John has built a sand castle! Like all good castles, the
walls have crennelations, that nifty pattern of embrasures (gaps)
and merlons (filled spaces); see the diagram below. The N (1 <= N
<= 25,000) merlons of his castle wall are conveniently numbered
1..N; merlon i has height M_i (1 <= M_i <= 100,000); his merlons
often have varying heights, unlike so many.
He wishes to modify the castle design in the following fashion: he
has a list of numbers B_1 through B_N (1 <= B_i <= 100,000), and
wants to change the merlon heights to those heights B_1, ..., B_N
in some order (not necessarily the order given or any other order
derived from the data).

To do this, he has hired some bovine craftsmen to raise and lower
the merlons' heights. Craftsmen, of course, cost a lot of money.
In particular, they charge FJ a total X (1 <= X <= 100) money per
unit height added and Y (1 <= Y <= 100) money per unit height
reduced.


FJ would like to know the cheapest possible cost of modifying his
sand castle if he picks the best permutation of heights. The answer
is guaranteed to fit within a 32-bit signed integer.

Note: about 40% of the test data will have N <= 9, and about 60% will have
N <= 18.

输入:

* Line 1: Three space-separated integers: N, X, and Y

* Lines 2..N+1: Line i+1 contains the two space-separated integers:
        M_i and B_i

输出:

* Line 1: A single integer, the minimum cost needed to rebuild the
        castle

输入样例:

3 6 5
3 1
1 2
1 2

输出样例:

11

提示:

INPUT DETAILS:

FJ's castle starts with heights of 3, 1, and 1. He would like to change
them so that their heights are 1, 2, and 2, in some order. It costs 6 to
add a unit of height and 5 to remove a unit of height.

OUTPUT DETAILS:

FJ reduces the first merlon's height by 1, for a cost of 5 (yielding
merlons of heights 2, 1, and 1). He then adds one unit of height
to the second merlon for a cost of 6 (yielding merlons of heights
2, 2, and 1).

问题 M: The Lost Cow【Easy】 时间限制: 1.000 Sec 内存限制: 128 MB 题目描述 Farmer John has lost his prize cow Bessie, and he needs to find her! Fortunately, there is only one long path running across the farm, and Farmer John knows that Bessie has to be at some location on this path. If we think of the path as a number line, then Farmer John is currently at position x and Bessie is currently at position y (unknown to Farmer John). If Farmer John only knew where Bessie was located, he could walk directly to her, traveling a distance of |x−y|. Unfortunately, it is dark outside and Farmer John can't see anything. The only way he can find Bessie is to walk back and forth until he eventually reaches her position. Trying to figure out the best strategy for walking back and forth in his search, Farmer John consults the computer science research literature and is somewhat amused to find that this exact problem has not only been studied by computer scientists in the past, but that it is actually called the "Lost Cow Problem" (this is actually true!). The recommended solution for Farmer John to find Bessie is to move to position x+1, then reverse direction and move to position x−2, then to position x+4, and so on, in a "zig zag" pattern, each step moving twice as far from his initial starting position as before. As he has read during his study of algorithms for solving the lost cow problem, this approach guarantees that he will at worst travel 9 times the direct distance |x−y| between himself and Bessie before he finds her (this is also true, and the factor of 9 is actually the smallest such worst case guarantee any strategy can achieve). Farmer John is curious to verify this result. Given x and y, please compute the total distance he will travel according to the zig-zag search strategy above until he finds Bessie. 输入 The single line of input contains two distinct space-separated integers x and y. Both are in the range 0…1,000. 输出 Print one line of output, containing the distance Farmer John will travel to reach Bessie. 样例输入 Copy 3 6 样例输出 Copy 9 模拟问题,用C++
最新发布
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值