参考链接:https://blog.csdn.net/SSLGZ_yyc/article/details/81700623

对顶栈的思想:

建立两个栈,栈A存储从序列开头到当前光标的位置的一段序列,栈B存储从光标到结尾的序列。这两个栈一共存储了整个序列。

java版本代码

import java.util.Scanner;
import java.util.Stack; public class Main {
public static void main(String[] args) {
int f[]=new int[200000];
f[0]=-999999999;
int sum=0;
Scanner in = new Scanner(System.in);
int n=in.nextInt();
while(in.hasNext())
{
Stack<Integer> a=new Stack<Integer>();
Stack<Integer> b=new Stack<Integer>(); while((n--)!=0) {
String s=in.next();
if (s.equals("I")) {
int x=in.nextInt();
a.push(x);
sum=sum+x;
int l=a.size();
f[l]=Math.max(sum, f[l-1]);
}else if (s.equals("D")&&a.size()>0) {
int x=a.pop();
sum=sum-x;
}else if (s.equals("L")&&a.size()>0) {
int x=a.pop();
sum=sum-x;
b.push(x);
}else if (s.equals("R")&&b.size()>0) {
int x=b.pop();
a.push(x);
sum+=x;
int l=a.size();
f[l]=Math.max(sum,f[l-1]);
}else if (s.equals("Q")) {
int x=in.nextInt();
System.out.println(f[x]);
}
}
}
}
}

 c++版本代码:

#include<iostream>
#include<stack>
#include<stdio.h>
using namespace std;
int f[2000000]; int main()
{
char ch,zfc[200];
int n;
while (scanf("%d",&n)!=EOF)
{
stack <int> a;
stack <int> b;
int sum=0,x;
f[0]=-999999999;
while (n--)
{
scanf("%s",zfc);
ch=zfc[0];
if (ch=='I')
{
scanf("%d",&x);
a.push(x);
sum+=x;
int l=a.size();
f[l]=max(sum,f[l-1]);
} else
if (ch=='D'&&a.size()>=1)
{
x=a.top();
a.pop();
sum-=x;
} else
if (ch=='L'&&a.size()>=1)
{
x=a.top();
a.pop();
sum-=x;
b.push(x);
} else
if (ch=='R'&&b.size()>=1)
{
x=b.top();
b.pop();
a.push(x);
sum+=x;
int l=a.size();
f[l]=max(sum,f[l-1]);
} else
if (ch=='Q')
{
scanf("%d",&x);
printf("%d\n",f[x]);
}
}
}
return 0;
}

  

 

0x11栈之Editor的更多相关文章

  1. 0x11栈之火车进栈

    参考<算法竞赛进阶指南>p.49 题目链接:https://www.acwing.com/problem/content/description/131/ 递推与递归的宏观描述 对于一个待 ...

  2. 0x11 栈

    这个不难吧,算是常识了..毕竟也是刷过USACO的人 对顶栈这东西前几天才遇到过,好像和在线求中位数那东西放一起了吧 单调栈倒是没什么...贴个代码算了.一开始有点蠢的每个位置算,后来发现出栈再算就行 ...

  3. 2019CSP-S初赛知识点汇总

    0x00 基本算法 0x01 位运算 0x02 前缀和与差分 0x03 二分 0x04 倍增 0x05 排序 0x06 离散化 0x07 高精度 0x10 数据结构 0x11 栈和队列 0x12 链表 ...

  4. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  5. HDOJ 4699 Editor 栈 模拟

    用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  6. hdu 4699 Editor 模拟栈

    思路:刚开始用STL中的栈,一直RE……,之后改为手动模拟栈操作,在注意点细节就可以了!!! 代码如下: #include<cstdio> #include<cstring> ...

  7. hdu4699 Editor 2013 多校训练第十场 D题 数列维护 splay | 线段树 | 栈!!!!!

    题意:维护一个文本编辑,并且查询最大前缀和. 写了splay,wa了13次 过了之后觉着特傻逼.发现题解两个栈就可以了,光标前后维护两个栈,维护前面的栈的前缀和 和 最大前缀和. 哎,傻逼,太弱了,还 ...

  8. [HDU4669]Editor (栈)

    题意 模拟编辑器,还是给链接吧 https://vjudge.net/problem/HDU-4699 思路 两个栈 代码 //poj1050 //n^4暴力 #include<algorith ...

  9. Editor HDU - 4699 (栈)

    Problem Description   Sample Input 8 I 2 I -1 I 1 Q 3 L D R Q 2   Sample Output 2 3 Hint The followi ...

随机推荐

  1. How to set font and colors of Eclipse UI

    The original URL of this article is https://codeyarns.com/2014/11/03/how-to-set-font-and-font-size-o ...

  2. 1.7Oob封装 继承 访问修饰符 静态和构造方法的执行顺序

    1:访问修饰符 private     同类中 默认        同类        同包 protect    同类         同包      子类 public     同类        ...

  3. python中list添加元素的方法append()、extend()和insert()

    append()函数:将新元素追加到列表末尾 In [1]: a = [1, 2, 3, 4, 5] In [2]: a.append(6) In [3]: a Out[3]: [1, 2, 3, 4 ...

  4. delphi调用windows自带语音功能

    windows自带语音接口 SAPI.SpVoice, 接口说明如下 https://docs.microsoft.com/en-us/previous-versions/windows/deskto ...

  5. C++ 第二次实验

    实验内容: 1.函数重载编程练习 编写重载函数add(),实现对int型,double型,Complex型数据的加法.在main()函数中定义不同类型 数据,调用测试. #include <io ...

  6. rabbit_mq实现分布式事务

    gitlab下载地址: 一.rabbitmq实现原理 一般在自己内部系统中建议采用lcn刚性事务来处理,面对调用第三方接口,或者夸平台语言是采用消息中间来实现补偿型事务.注意在进行补偿时需要注意重复调 ...

  7. GRUB 的配置文件解析

    原文:http://c.biancheng.net/view/1032.html 本节,我们就来看看 GRUB 的配置文件 /boot/gmb/grub.conf 中到底写了什么.命令如下: [roo ...

  8. awk 实战

    awk 一些好玩的用法.有什么不错的点子可以留言,发挥出awk牛逼功能 分离mac地址 ifconfig wlan0 | grep eth | awk '{n=split($2,arr,": ...

  9. [svc]tcp三次握手四次挥手&tcp的11种状态(半连接)&tcp的time-wait

    TCP的状态转化过程(11种状态)以及TIME_WAIT状态 高性能网络 | 你所不知道的TIME_WAIT和CLOSE_WAIT 我相信很多都遇到过这个问题.一旦有用户在喊:网络变慢了.第一件事情就 ...

  10. python类与对象-如何让对象支持上下文管理

    如何让对象支持上下文管理 问题举例 一个telnet客户端的类TelnetClient, 调用实例的connect(),login(),interact方法 启动客户端与服务器交互,交互完毕后需要调用 ...