当前位置:网站首页>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;
}
边栏推荐
- Codeforces Round #645 (Div. 2)
- Recyclerview refreshes blinks and crashes when deleting items
- Shanke's C language 2018 exercise (Telecom)
- 内网穿透工具frp使用入门
- 1147 Heaps (30 分)
- Reverse thinking - short story
- GridView of basic component of shutter
- 时变和非时变
- Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
- Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
猜你喜欢

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

Codeforces Round #645 (Div. 2)

EDA与VHDL题库

2020-09-29 非商品模板化代码层次 rapidjson库

Gmail: how to quickly read all messages

Pipeline details of IPC (interprocess communication)

Flutter 基础组件之 Text

How to traverse objects in the vector container

IPC(进程间通信)之管道详解

KDevelop new project
随机推荐
详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
Reverse thinking - short story
F5 BIG-IP iControl REST命令执行(CVE-2022-1388)
Sixteen system counter and flow lamp
逆向思维-小故事
Signal works: time varying and time invariant
JVM之TLAB
Leetcode MySQL database topic 181
2019.10.30学习总结
Recyclerview refreshes blinks and crashes when deleting items
Alternative implementation of Scrollview pull-down header amplification
Application of keil5 integrated development environment for single chip microcomputer
同花顺炒股软件可靠吗,安全吗?
FreeRTOS (VIII) - time management
C语言中通过sprintf()函数构造sql语句
聊聊你理解的线程与并发
51nod1277 字符串中的最大值【KMP】
KDevelop new project
Wechat applet realizes store function
HDU 4578 Transformation(线段树+有技巧的懒标记下放)