AtCoder题解 —— AtCoder Beginner Contest 182 —— D - Wandering

题目相关

题目链接

AtCoder Beginner Contest 182 D 题,https://atcoder.jp/contests/abc182/tasks/abc182_d

Problem Statement

Given is a number sequence A1,A2,A3,…,AN, which may contain negative elements.
On a number line, there is a robot at coordinate 0 . It will do the following actions in order:

  • Move A1 in the positive direction.
  • Move A1 in the positive direction, and then move A2 in the positive direction.
  • Move A1 in the positive direction, then move A2 in the positive direction, and then move A3 in the positive direction.
  • Move A1 in the positive direction, then move A2 in the positive direction, then move A3 in the positive direction, …, …, and then move AN in the positive direction.

Find the greatest coordinate occupied by the robot from the beginning to the end of the process.

Input

Input is given from Standard Input in the following format:

N
A1 A2 A3 … AN

Output

Print the greatest coordinate occupied by the robot from the beginning to the end of the process.

Samples1

Sample Input 1

3
2 -1 -2

Sample Output 1

5

Explaination

The robot moves as follows:

  • Move 2 in the positive direction, to coordinate 2.
  • Move 2 in the positive direction, to coordinate 4. Then move −1 in the positive direction, to coordinate 3.
  • Move 2 in the positive direction, to coordinate 5. Then move −1 in the positive direction, to coordinate 4. Then move −2 in the positive direction, to coordinate 2.

The greatest coordinate occupied during the process is 5, so we should print 5.

Samples2

Sample Input 2

5
-2 1 3 -1 -1

Sample Output 2

2

Samples3

Sample Input 3

5
-1000 -1000 -1000 -1000 -1000

Sample Output 3

0

Explaination

In this case, the initial coordinate 0 is the greatest coordinate occupied.

Constraints

  • 1≤N≤200000
  • −10^8≤Ai≤10^8
  • All values in input are integers.

题解报告

题目翻译

给一个由 A1, A2, A3, ..., An 组成的序列,其中可能包括负数。有一个机器人在坐标 0 位置,机器人将按照顺序进行如下动作:

  • 将 A1 向正方向移动。
  • 将 A1 向正方向移动,接着将 A2 向正方向移动。
  • 将 A1 向正方向移动,接着将 A2 向正方向移动,接着将 A3 向正方向移动。
  • ...
  • 将 A1 向正方向移动,接着将 A2 向正方向移动,接着将 A3 向正方向移动,...,...,最后将 An 向正方向移动。

请找出机器人能移动到最大坐标。

样例数据分析

样例数据 1

根据样例,序列 A 为 {2 -1 -2}。因为要移动,我们将数据做一个前缀和数组 nums(前缀和是常用的数据预处理技巧),这样数据变为 {0 2 1 -1}。同时我们来一个最大值数组 maxx,该数组保存前缀和前 i-1 个数据和 a[i] 的最大值。这样我们还有一个 maxx 数组 {0 2 2 2}。由于 n=3,因此机器人只能移动三次。这样我们利用下面的表格进行分析。

由于机器人只能向正方向移动,因此机器人每次可能的位置为:当前位置和可移动位置的最大值,也就是 ans=max(ans, pos+maxx[i]);

可移动位置就是当前位置加上 nums[i],也就是 pos = pos+nums[i];

 ansposnums[i]maxx[i]now+maxx[i]
初始0000 
122220+2=2
243122+2=4
354-123+2=5

这样,经过三次移动后,机器人的位置 ans=5。

样例数据 2

根据样例,序列 A 为 {-2 1 3 -1 -1}。我们将数据做一个前缀和数组 nums,这样数据变为 {0 -2 -1 2 1 0}。同时我们来一个最大值数组 maxx,该数组保存前缀和前 i-1 个数据和 a[i] 的最大值。这样我们还有一个 maxx 数组 {0 0 0 2 2 2}。由于 n=5,因此机器人只能移动五次。这样我们利用下面的表格进行分析。

 ansposnums[i]maxx[i]now+maxx[i]
初始0000 
10-2-200
20-3-10-3
30-122-1
410121
520022

这样,经过五次移动后,机器人的位置 ans=2。

样例数据 3

根据样例,序列 A 为 {-1000 -1000 -1000 -1000 -1000}。我们将数据做一个前缀和数组 nums,这样数据变为 {0 -1000 -2000 -3000 -4000 -5000}。同时我们来一个最大值数组 maxx,该数组保存前缀和前 i-1 个数据和 a[i] 的最大值。这样我们还有一个 maxx 数组 {0 0 0 0 0 0}。由于 n=5,因此机器人只能移动五次。这样我们利用下面的表格进行分析。

 ansposnums[i]maxx[i]now+maxx[i]
初始0000 
10-1000-100000
20-3000-20000-1000
30-6000-30000-3000
40-10000-40000-6000
50-15000-50000-10000

这样,经过五次移动后,机器人的位置 ans=0。

题目分析

从上面的数据分析可以看出,我们通过前缀和来降低运算复杂度,维护一个 maxx 数组来确认最优的位置。为了更清楚的看出数据,这样我们可以写出以下核心:

a[i] 用来保存原始数据

nums[i] = a[0]+a[1]+...+a[i] = nums[i-1]+a[i];

maxx[i] = max(nums[0], nums[1], ..., nums[i]) = max(maxx[i-1], nums[i]);

机器人可以到的位置是 nums[0]+nums[1]+nums[2]+...+nums[n];

最大的距离是 max(0, maxx[0], nums[0]+maxx[1], nums[0]+nums[1]+maxx[2], ...)。

数据分析

N 的范围是 [1, 2e5],数据 Ai 范围是 [-1e8, 1e8],因此最大的数据是 2e5*1e8=2e13,因此需要使用 long long 来表示。

AC 参考代码

//https://atcoder.jp/contests/abc182/tasks/abc182_d
//D - Wandering
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MAXN=1e6+4;
LL nums[MAXN];//采用前缀和预处理
LL maxx[MAXN];

int main() {
#if 1
    //提交OJ使用快读
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    LL n;
    cin>>n;
    for (LL i=1; i<=n; i++) {
        cin>>nums[i];
        //计算前缀和
        nums[i] += nums[i-1];
        maxx[i] = max(maxx[i-1], nums[i]);
    }

    LL ans=0;//当前robot位置
    LL pos=0;
    for (LL i=1; i<=n; i++) {
        ans=max(ans, pos+maxx[i]);
        pos += nums[i];
    }

    cout<<ans<<"\n";

    return 0;
}

时间复杂度

O(N)。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
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$,如果存在,则无法完成任务。否则,可以用贪心的思想,每次从物品数量最多的盒子中取出一个物品,放入物品数量最少的盒子中。因为每次操作都会使得物品数量最多的盒子的物品数量减少,而物品数量最少的盒子的物品数量不变或增加,因此这种贪心策略可以保证最少需要的操作次数最小。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力的老周

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

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

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

打赏作者

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

抵扣说明:

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

余额充值