mthoutai

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理


用两个栈模拟:

Editor

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1913    Accepted Submission(s): 591


Problem Description
 

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

Sample Output
2 3
Hint
The following diagram shows the status of sequence after each instruction:
 

Source
 



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>

using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=1001000;

int Left[maxn],Right[maxn];
int sum[maxn],maxsum[maxn];
int nl,nr;

char op[10];
int x,T_T,sz;

void init()
{
  nl=0; nr=0;
  maxsum[0]=-INF;
  sum[0]=0;
  sz=1;
}

int nextInt()
{
  bool ok=false;
  int ret=0; char ch;
  int xi=0;
  while(ch=getchar())
    {
      if(ch=='-'||(ch>='0'&&ch<='9'))
        {
          ok=true;
          if(ch=='-') xi=1;
          else ret=ret*10+ch-'0';
        }
      else if(ok==true) break;
    }
  if(xi) ret*=-1;
  return ret;
}

char nextChar()
{
  char ch=0;
  while(ch=getchar())
    {
      if(ch=='D'||ch=='R'||ch=='L'||ch=='Q'||ch=='I')
        {
          return ch;
        }
    }
}

int main()
{
  while(scanf("%d",&T_T)!=EOF)
    {
      init();
      while(T_T--)
        {
          op[0]=nextChar();
          if(op[0]=='I')
            {
              x=nextInt();
              Left[nl++]=x;
              sum[sz]=sum[sz-1]+x;
              maxsum[sz]=max(maxsum[sz-1],sum[sz]);
              sz++;
            }
          else if(op[0]=='D')
            {
              if(nl==0) continue;
              nl--;
              sz--;
            }
          else if(op[0]=='L')
            {
              if(nl==0) continue;
              int t=Left[nl-1];
              nl--;
              Right[nr++]=t;
              sz--;
            }
          else if(op[0]=='R')
            {
              if(nr==0) continue;
              int t=Right[nr-1];
              nr--;
              Left[nl++]=t;
              sum[sz]=sum[sz-1]+t;
              maxsum[sz]=max(maxsum[sz-1],sum[sz]);
              sz++;
            }
          else if(op[0]=='Q')
            {
              int x;
              x=nextInt();
              printf("%d\n",maxsum[x]);
            }
        }
    }
  return 0;
}





posted on 2017-04-21 08:53  mthoutai  阅读(203)  评论(0编辑  收藏  举报