当前位置:网站首页>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;
}
边栏推荐
- Get array median
- Mysql5.6.24 installation free deployment method
- Malicious code analysis practice - lab03-02 DLL analysis
- PHP specifies the number of people to distribute the specified amount equally at random (scaling method)
- Vscode code debugging skills
- Common configuration commands for Cisco network device security management
- Is the acceptance standard a test case?
- Binassii module - converting between binary and ASCII
- VSCode代码调试技巧
- PHP can load different new data methods for each refresh
猜你喜欢

How to play the 2022 Taobao 618 Super Cat Games? Playing skills of 2022 Taobao 618 Cat Games

M-Arch(番外12)GD32L233评测-CAU加解密(捉弄下小编)

AcWing 135. Maximum subsequence sum (prefix sum + monotone queue to find the minimum value of fixed length interval)

ArrayList automatic capacity expansion principle

数组,整型,字符变量在全局和局部的存在形式

Bug easily ignored by recursion

The most detailed explanation of the top ten levels of sqli labs platform

This and final keywords

How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
![[machine learning] practice of logistic regression classification based on Iris data set](/img/c6/0233545d917691b8336f30707e4636.png)
[machine learning] practice of logistic regression classification based on Iris data set
随机推荐
MYSQL——内置函数
PHP wechat red packet allocation logic
How the ArrayList collection implements ascending and descending order
Leetcdoe 2037. Make each student have the minimum number of seat movements (yes, once)
元宇宙系统搭建与构造
How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?
Fiddler automatically saves the result of the specified request to a file
Solution to invalid small program scroll into view
[machine learning] practice of logistic regression classification based on Iris data set
2022淘宝618超级喵运会玩法攻略 618超级喵运会玩法技巧
数组,整型,字符变量在全局和局部的存在形式
Malicious code analysis practice - lab03-01 Exe basic dynamic analysis
The most detailed explanation of the top ten levels of sqli labs platform
Introduction to encoding formats (ASCII, Unicode and UTF-8)
浅谈调和形状上下文特征HSC对3DSC的改进
Common regular expressions
AcWing 135. 最大子序和(前缀和 + 单调队列求定长区间最小值)
Collation of common functions in JS
PHP download station B video
PHP: Excel to get the letter header