当前位置:网站首页>Wandering --- 最后的编程挑战
Wandering --- 最后的编程挑战
2022-06-29 09:23:00 【陌陌623】
要走N次:
样例:
3
2 -1 -2
————————————————
第一次走2
第二次 先走2 再走-1
第三次 先走2 再走-1 再走 -1
————————————————
从上面的三次行动中找出最远可以到的距离:
就变成了这样:
2 ———— 2 选2
2 -1 ———— 3 选2为4,走到第三步仍需要选择 -1 — 3
2 -1 -1 ———— 5 选2为5
可以发现,想走到第三步,必须将第2步全部走完。而我们要的是这些步中
最远的那个。
如果是普通算法,可以直接 O ( N 2 ) O(N^2) O(N2)遍历,但肯定是超时
所以我们有两个需要解决的问题:
- 每一步的最优解
- 怎么快速到达下一步
即: - 使用一个辅助数组,记录到i步的,最远的位置
- 快速到达下一步使用前缀和
可以优化成 O ( N ) O(N) O(N)
#include <iostream>
using namespace std;
using LL = long long;
const int n = 200002;
LL a[n];
LL b[n];
int N;
int main()
{
cin >> N;
for (int i = 1; i <= N; i++)
scanf("%lld", &a[i]), a[i] += a[i - 1], b[i] = max(b[i - 1], a[i]);
// 答案
LL maxt = 0;
// 当前的位置
LL sum = 0;
for (int i = 1; i <= N; i++)
{
// 选择这次运动的最优位置
maxt = max(maxt, sum + b[i]);
// 如果要进行下次运动必要要将这次的全部走完
sum = sum + a[i];
}
cout << maxt;
return 0;
}
边栏推荐
- Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
- 2019.11.13训练总结
- 分布式和集群分不清,我们讲讲两个厨子炒菜的故事
- C语言中通过sprintf()函数构造sql语句
- Using rancher to build kubernetes cluster
- Dynamic linking of virtual machine stack of JVM
- setInterval、setTimeout和requestAnimationFrame
- 2019.11.3学习总结
- 阿里云防火墙配置,多种设置方式(iptables和fireward)
- 信号作品:时变和时不变
猜你喜欢

nacos注册中心集群

自定义控件之侧滑关闭 Activity 控件

Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks

A method of creating easy to manage and maintain thread by C language

点在多边形内外的判断

SeaweedFS安全配置(Security Configuration)

Constructing SQL statements by sprintf() function in C language

FreeRTOS(九)——队列

float 与 int 相乘产生的令人崩溃的“ 2.3 * 10 = 22 ”

Codeforces Round #659 (Div. 2)
随机推荐
Codeforces Round #652 (Div. 2)
TLAB of JVM
Dsolve function of sympy
RecyclerView刷新闪烁与删除Item时崩溃问题
F5 BIG-IP iControl REST命令执行(CVE-2022-1388)
sublime text3设置运行自己的Makefile
2019-11-10训练总结
Caused by: org. apache. xerces. impl. io. MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8
Perfect binary tree, complete binary tree, perfect binary tree
Signal works: time varying and time invariant
setInterval、setTimeout和requestAnimationFrame
2019.11.13训练总结
Codeforces Round #645 (Div. 2)
manacher
图片验证码控件
【51nod 1215】数组的宽度
HDU 6778 Car (分组枚举-->状压 dp)
A method of creating easy to manage and maintain thread by C language
520 钻石争霸赛 2021
GridView of basic component of shutter