AcWing Editor

AcWing Editor

Description

  • You are going to achieve a powerful sequence of integers editor.

    In the beginning, the sequence is empty.

    There are five command editor, as follows:

    1, "I x", x value is inserted at the cursor.
    2, "D", the first element in front of the cursor to delete, if there is no front element, this action is ignored.
    3, "L", the cursor moves to the left, a skip element, if there is no element left, this operation is ignored.
    4, "R", the cursor moves to the right, to skip an element, if not the right element is ignored operations.
    5, "Q k", assuming sequence before the cursor is at the moment A . 1, A 2, ..., AN

    Output, wherein = Si A 1+ A 2 + ... + AI

Input

  • The first row contains an integer Q, it represents the total number of instructions.

    Next Q lines, each row one instruction, the instruction format as specifically described in the subject.

Output

  • Each "Q k" command, the output of an integer as a result, one row for each result.

Sample Input

8
I 2
I -1
I 1
Q 3
L
D
R
Q 2

Sample Output

2
3

Data Size

  • 1≤Q≤106,
    |x|≤103,
    1≤k≤n

answer:

  • Stack.
  • Also remember k-demand dynamic heap on top of the big time with it? This problem can be understood as the image "on top of the stack."
  • Providing two stacks, a number of storage left of the cursor of the stack, the stack 2 stored right of the cursor.
  • The cursor is to move around the stack top half shells off into another stack.
  • Delete is a direct bomb off the top of the stack 1.
  • Query prefix and the maximum is easy. Principle with this case. Specific sum is to open two arrays and ans. Real-time maintenance prefix sum stack 1 and, ans sum of the value of most real-time maintenance.
#include <iostream>
#include <cstdio>
#include <stack>
#include <queue>
#define N 1000005
using namespace std;

int n;
stack<int> stk1, stk2;
int sum[N], ans[N] = {-0x7fffffff};

int read()
{
    int x = 0, f = 1; char c = getchar();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x *= f;
}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        char s[3]; scanf("%s", s);
        if(s[0] == 'I')
        {
            int x = read();
            stk1.push(x);
            int p = stk1.size();
            sum[p] = sum[p - 1] + x;
            ans[p] = max(ans[p - 1], sum[p]);
        }
        else if(s[0] == 'D' && stk1.size()) stk1.pop();
        else if(s[0] == 'L' && stk1.size())
        {
            stk2.push(stk1.top());
            stk1.pop();
        }
        else if(s[0] == 'R' && stk2.size())
        {
            stk1.push(stk2.top());
            int p = stk1.size();
            sum[p] = sum[p - 1] + stk2.top();
            ans[p] = max(ans[p - 1], sum[p]);
            stk2.pop();
        }
        else if(s[0] == 'Q')
        {
            int x = read();
            printf("%d\n", ans[x]);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11300719.html