当前位置:网站首页>AcWing 128. 编辑器(对顶栈 实现序列内部指定位置高效修改)
AcWing 128. 编辑器(对顶栈 实现序列内部指定位置高效修改)
2022-06-12 10:32:00 【Jacob* ̄▽ ̄*】



我们用这道题引入一个新的概念:对顶栈。(类似的,还有 对顶堆,日后我们会谈到)
思路:
本题的特殊点在于:I,D,L,R 四种操作 都在 光标位置处 发生,并且操作完成后,光标至多移动 1 个位置。
根据这种 “始终在 序列中间某个指定位置 进行 修改” 的性质,我们想到了 “对顶栈” 。
做法
建立两个栈:
栈
lestk存储 从 序列开头 到 当前光标位置 的这一段子序列。栈
ristk存储 从 当前光标位置 到 序列结尾 的这一段子序列。
二者都以光标所在的那一端作为栈顶。这两个栈合起来就保存了整个序列。
因为 查询操作的 k不超过光标位置,所以我们用一个 数组 ans 维护栈 lestk 的 前缀和 的 最大值。
此外,设 lestk 的栈顶位置下标是 idx,sum 是序列 lestk 的前缀和数组。
(1)对于 I x 操作:
- 1.把
x插入栈lestk; - 2.更新
sum[idx] = sum[idx - 1] + lestk[idx]; - 3.更新
ans[idx] = max(ans[idx - 1], sum[idx])。
(2)对于 D 操作,把 lestk 的栈顶出栈。
(3)对于 L 操作,弹出 lestk 的栈顶,插入到 ristk 中。
(4)对于 R 操作:
- 1.弹出
ristk的栈顶,插入到lestk中; - 2.更新
sum[idx] = sum[idx - 1] + lestk[idx]; - 3.更新
ans[idx] = max(ans[idx - 1], sum[idx])。
(5)对于 Q k 询问,直接返回 ans[k]。
通过这样两个 “对顶栈”,我们在 O(1) 的时间内 实现了每种操作和询问。
时间复杂度:
O ( 1 ) O(1) O(1)
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
stack<int> lestk, ristk;
int sum[N];
int ans[N];
int main()
{
int T = 1; //cin >> T;
while (T--)
{
memset(ans, -0x3f, sizeof ans);
int q; cin >> q;
int idx = 0;
while (q--)
{
char op[2];
cin >> op;
if (*op == 'I') {
int x; cin >> x;
idx++;
lestk.push(x);
sum[idx] = sum[idx - 1] + x;
//cout << "sum[idx] idx " << sum[idx] << ' ' << idx << endl;
ans[idx] = max(ans[idx - 1], sum[idx]);
}
else if (*op == 'D') {
if (lestk.size()) {
sum[idx] -= lestk.top();
ans[idx] = max(ans[idx - 1], sum[idx]);
idx--;
lestk.pop();
}
}
else if (*op == 'L') {
if (lestk.size()) {
idx--;
int top = lestk.top();
lestk.pop();
ristk.push(top);
}
}
else if (*op == 'R') {
if (ristk.size()) {
idx++;
int top = ristk.top();
sum[idx] = sum[idx - 1] + top;
ans[idx] = max(ans[idx - 1], sum[idx]);
ristk.pop();
lestk.push(top);
}
}
else if (*op == 'Q') {
int k; cin >> k;
cout << ans[k] << '\n';
}
}
}
return 0;
}
边栏推荐
- [MySQL] index invalidation and index optimization
- 2021-03-24
- Mobile terminal commissioning
- Wechat payment, wechat refund, Alibaba payment
- PLC如何自行构造移位功能块(FC)
- 2022淘宝618超级喵运会怎么玩?2022淘宝618喵运会玩法技巧
- Leetcdoe 2037. 使每位学生都有座位的最少移动次数(可以,一次过)
- Solution to invalid small program scroll into view
- A few secrets - a special day
- Composer command
猜你喜欢

A snare - Cookie spoofing

CONDA install tensorflow test tensorflow

Building 64 bit wampserver and DVWA in win7 virtual machine

2. factory mode

On the improvement of 3dsc by harmonic shape context feature HSC

Leetcode 2169. 得到 0 的操作数

机器学习不是你想用,想用就能用

Tp6+memcached configuration

Mqtt protocol Chinese version

基于C#的安全聊天工具设计
随机推荐
Php:redis uses geospatial
3. Abstract Factory
Index query efficiency of MySQL
93. 獲得內網的所有IP地址
Several methods of importing ThinkPHP
Oculus quest generation opens Bluetooth connection
Pagoda chevereto1.6.2 the latest version of stepping on the pit tutorial in Chinese
看看你有没有陷入“标签化”客户和 用户 的陷阱?
What can QA do in a "de QA" project?
淺談調和形狀上下文特征HSC對3DSC的改進
2022淘宝618超级喵运会怎么玩?2022淘宝618喵运会玩法技巧
在一个“去QA化”的项目中,QA能做什么?
PHP uses leanclound to save associated properties
PHP wechat red packet allocation logic
Valentina Studio Pro for MAC (MAC database management software)
Cookie object
Chapter 3 search
conda 安装tensorflow 测试tensorflow
2021-03-24
properties中文乱码