【HDOJ】4699 Editor
【题目】 http://acm.hdu.edu.cn/showproblem.php?pid=4699
【报告】
模拟题吧,本身不是很难。
最初的想法是用Splay tree写,然后疯掉了。苏牛说2B,直接两个栈就能搞定的还用Splay tree,然我我无奈了。。。
用两个栈维护,一边是光标左边,一边是光标右边。当光标左边的栈插入或者删除的时候同时维护栈底到栈顶的和还有最大和,就O了。
【程序】
// Task: 4699 Editor
// Designer: Rsky 2013/08/27
#include
#include
#include
#include
#include
#include
using namespace std;
char blank[100];
stack a,b; //
A:光标前,B:光标后,
int an,bn;
vector s,m; // S:1~当前的和,M:1~当前的最大和
inline void insert(int x) //
插入
{
a.push(x);
// 压入左边栈
s.push_back(x+s[s.size()-1]); // 计算全部和
m.push_back(max(s[s.size()-1],m[m.size()-1])); //
计算最大和
an++;
}
inline void
erase()
// 删除
{
if
(an>0)
{
a.pop();
s.pop_back();
m.pop_back();
an--;
}
}
inline void
tleft()
// 左移
{
if
(an>0)
{
b.push(a.top());
// 左边栈顶入右边栈
bn++;
erase();
// 删掉左边栈顶
}
}
inline void
tright()
// 右移
{
if
(bn>0)
{
insert(b.top());
b.pop();
bn--;
}
}
inline void query(int
x) //
询问
{
// cout << m[x] <<
endl;
printf("%d\n",m[x]);
}
int main()
{
int n;
while
(scanf("%d",&n)!=EOF)
{
// gets(blank); // 读掉行尾空格
char ch;
int x;
while (!a.empty()) a.pop();
while (!b.empty()) b.pop();
s.clear();m.clear();
// 清空
a.push(0);b.push(0);s.push_back(0);m.push_back(-2147483647); //
栈底,防溢出
an=bn=0;

浙公网安备 33010602011771号