当前位置:网站首页>AcWing 128. Editor (to effectively modify the specified position in the top stack)
AcWing 128. Editor (to effectively modify the specified position in the top stack)
2022-06-12 10:40:00 【Jacob* ̄▽ ̄*】



We use this problem to introduce a new concept : The top of the stack .( Allied , also To the top pile , We will talk about )
Ideas :
The special point of this question is :I,D,L,R Four operations All in At the cursor position happen , And when it's done , The cursor moves at most 1 A place .
According to this “ Always in A specified position in the middle of the sequence Conduct modify ” The nature of , We thought about it “ The top of the stack ” .
practice
Set up two stacks :
Stack
lestkStorage from The beginning of the sequence To Current cursor position This sub sequence of .Stack
ristkStorage from Current cursor position To End of sequence This sub sequence of .
Both use the end of the cursor as the top of the stack . Together, these two stacks save the entire sequence .
because Query operation k Do not exceed the cursor position , So we use a Array ans Maintenance stack lestk Of The prefix and Of Maximum .
Besides , set up lestk The subscript at the top of the stack is idx,sum It's a sequence lestk Prefix and array .
(1) about I x operation :
- 1. hold
xInsert stacklestk; - 2. to update
sum[idx] = sum[idx - 1] + lestk[idx]; - 3. to update
ans[idx] = max(ans[idx - 1], sum[idx]).
(2) about D operation , hold lestk Top stack of .
(3) about L operation , eject lestk Top of stack , Insert into ristk in .
(4) about R operation :
- 1. eject
ristkTop of stack , Insert intolestkin ; - 2. to update
sum[idx] = sum[idx - 1] + lestk[idx]; - 3. to update
ans[idx] = max(ans[idx - 1], sum[idx]).
(5) about Q k inquiry , Go straight back to ans[k].
Through these two “ The top of the stack ”, We are O(1) In time Each operation and inquiry is realized .
Time complexity :
O ( 1 ) O(1) O(1)
Code :
#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;
}
边栏推荐
- Flex layout
- Valentina Studio Pro for MAC (MAC database management software)
- Common methods of string class
- Amélioration de la 3dsc par HSC
- Distributed storage exploration
- 力扣(LeetCode)162. 寻找峰值(2022.06.11)
- On the improvement of 3dsc by harmonic shape context feature HSC
- PHP wechat red packet allocation logic
- The name of a great man
- 深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)
猜你喜欢

Global and local existence of array, integer and character variables

Common methods of string class

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

Mqtt protocol Chinese version

Malicious code analysis practice - lab03-02 DLL analysis

Class. Forname connection MySQL driver keeps throwing classnotfoundexception exception solution

基于C#的安全聊天工具设计

Pseudo static setting of access database in win2008 R2 iis7.5

Malicious code analysis practice - lab06-01 exe~Lab06-04. Exe dynamic analysis

Amélioration de la 3dsc par HSC
随机推荐
Zabbix 监控之LLD
Failed to load resource: the server responded with a status of 413 (Request Entity Too Large)
分布式存储探索
浅谈三维形状上下文特征3DSC理论及应用
【CF1392D】D. Omkar and Bed Wars(环形与后效性dp)
AcWing 128. 编辑器(对顶栈 实现序列内部指定位置高效修改)
PHP: Excel to get the letter header
Introduction to encoding formats (ASCII, Unicode and UTF-8)
Pycharm view the current version of opencv
FPGA-按键实验
Immer source code reading
2022京東618預售定金怎麼退?京東618定金能退嗎?
PHP download station B video
蓝桥杯2015年CA省赛(填坑中)
2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法
Malicious code analysis practice - lab03-03 Exe basic dynamic analysis
2022淘宝618超级喵运会怎么玩?2022淘宝618喵运会玩法技巧
How the ArrayList collection implements ascending and descending order
AcWing 132. 小组队列(队列模拟题)
Distributed storage exploration