AtCoder题解 —— AtCoder Regular Contest 109 —— A - Hands

题目相关

题目链接

AtCoder Regular Contest 109 A 题,https://atcoder.jp/contests/arc109/tasks/arc109_a

Problem Statement

There are two 100-story buildings, called A and B. (In this problem, the ground floor is called the first floor.)

For each i=1,…,100 the i-th floor of A and that of B are connected by a corridor. Also, for each i=1,…,99, there is a corridor that connects the (i+1)-th floor of A and the i-th floor of B. You can traverse each of those corridors in both directions, and it takes you x minutes to get to the other end.

Additionally, both of the buildings have staircases. For each i=1,…,99, a staircase connects the i-th and (i+1)-th floors of a building, and you need yy minutes to get to an adjacent floor by taking the stairs.

Find the minimum time needed to reach the b-th floor of B from the a-th floor of A.

Input

Input is given from Standard Input in the following format:

a b x y

Output

Print the minimum time needed to reach the b-th floor of B from the a-th floor of A.

Samples1

Sample Input 1

2 1 1 5

Sample Output 1

1

Explaination

There is a corridor that directly connects the 2-nd floor of A and the 1-st floor of B, so you can travel between them in 1 minute. This is the fastest way to get there, since taking the stairs just once costs you 5 minutes.

Samples2

Sample Input 2

1 2 100 1

Sample Output 2

101

Explaination

For example, if you take the stairs to get to the 2-nd floor of A and then use the corridor to reach the 2-nd floor of B, you can get there in 1+100=101 minutes.

Samples3

Sample Input 3

1 100 1 100

Sample Output 3

199

Explaination

Using just corridors to travel is the fastest way to get there.

Constraints

  • 1≤a,b,x,y≤100
  • All values in input are integers.

题解报告

题目翻译

有两幢 100 层的大楼 A 和 B,本问题中,底楼称为 1 楼。在 A 和 B 的每一层之间都有通道连接,同时 A 的第 i+1 层和 B 的第 i 层有双向通道连接,需要发费 x 时间。同时大楼内部还有楼梯连接,从第 i+1 楼到第 i 楼需要发费 y 时间。

求从 B 的 b 层到 A 的 a 层需要使用的最少时间。

题目分析

又是一个友善的打卡数学题。

如上图所示,绿色表示大楼 A 和 B 之间的走廊,粉红表示大楼内部的楼梯。本题由于 x 和 y 是固定的,因此不会存在中间更换楼梯和通道的选择。注意通道是 A 的第 i+1 层到 B 的第 i 层这个小细节。我们可以发现两个细节

细节一

我们要从大楼 B 的第 3 层和大楼 B 的第 2 层,我们有两个选择:方案一从大楼 B 第 3 层走走廊到大楼 A 第 2 层,再从走廊走到大楼 B 的第 2 层,这样使用了 2x 时间;方案二从使用楼梯,大楼 B 第 3 层走走廊到大楼 B 第 2 层,这样使用了 y 的时间。因此对我们来说,选择最小的。也就是 min(y, 2x)。如下图所示。

细节二

要从大楼 B 到 大楼 A。至少需要走一次走廊。

AC 参考代码

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int a,b,x,y;
    cin >> a >> b >> x >> y;
	if(a > b) {
        cout << min(y,2*x)*max(0,abs(a-b)-1)+x;
    } else {
        cout << min(y,2*x)*max(0,abs(a-b))+x;
    }
    cout<<"\n";
    return 0;
}

时间复杂度

O(1)。

空间复杂度

O(1)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
AtCoder Beginner Contest 134 是一场 AtCoder 的入门级比赛,以下是每道的简要题解: A - Dodecagon 目描述:已知一个正十二边形的边长,求它的面积。 解思路:正十二边形的内角为 $150^\circ$,因此可以将正十二边形拆分为 12 个等腰三角形,通过三角形面积公式计算面积即可。 B - Golden Apple 目描述:有 $N$ 个苹果和 $D$ 个盘子,每个盘子最多可以装下 $2D+1$ 个苹果,求最少需要多少个盘子才能装下所有的苹果。 解思路:每个盘子最多可以装下 $2D+1$ 个苹果,因此可以将苹果平均分配到每个盘子中,可以得到最少需要 $\lceil \frac{N}{2D+1} \rceil$ 个盘子。 C - Exception Handling 目描述:给定一个长度为 $N$ 的整数序列 $a$,求除了第 $i$ 个数以外的最大值。 解思路:可以使用两个变量 $m_1$ 和 $m_2$ 分别记录最大值和次大值。遍历整个序列,当当前数不是第 $i$ 个数时,更新最大值和次大值。因此,最后的结果应该是 $m_1$ 或 $m_2$ 中较小的一个。 D - Preparing Boxes 目描述:有 $N$ 个盒子和 $M$ 个物品,第 $i$ 个盒子可以放入 $a_i$ 个物品,每个物品只能放在一个盒子中。现在需要将所有的物品放入盒子中,每次操作可以将一个盒子内的物品全部取出并分配到其他盒子中,求最少需要多少次操作才能完成任务。 解思路:首先可以计算出所有盒子中物品的总数 $S$,然后判断是否存在一个盒子的物品数量大于 $\lceil \frac{S}{2} \rceil$,如果存在,则无法完成任务。否则,可以用贪心的思想,每次从物品数量最多的盒子中取出一个物品,放入物品数量最少的盒子中。因为每次操作都会使得物品数量最多的盒子的物品数量减少,而物品数量最少的盒子的物品数量不变或增加,因此这种贪心策略可以保证最少需要的操作次数最小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值