zoukankan      html  css  js  c++  java
  • hdu 4669 模拟

    思路:

    主要就是模拟这些操作,用链表果断超时。改用堆栈模拟就过了

    #include<map>
    #include<set>
    #include<stack>
    #include<cmath>
    #include<queue>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define Maxn 1000010
    #define Maxm 300010
    #define LL __int64
    #define Abs(x) ((x)>0?(x):(-x))
    #define lson(x) (x<<1)
    #define rson(x) (x<<1|1)
    #define inf 0x7fffffff
    #define Mod 1000000007
    int Max[Maxn],sum[Maxn];
    using namespace std;
    stack<int> br,af;
    int main()
    {
        int i,j,u,v,n,x;
        char str[3];
        int brz,afz;
        while(scanf("%d",&n)!=EOF){
        brz=afz=0;
        while(!br.empty())
        br.pop();
        while(!af.empty())
        af.pop();
        sum[0]=0;
        Max[0]=-100000;
        for(i=1;i<=n;i++){
            scanf("%s",str);
            if(str[0]=='I'){
                scanf("%d",&x);
                br.push(x);
                sum[++brz]=sum[brz-1]+x;
                Max[brz]=max(Max[brz-1],sum[brz]);
            }
            if(str[0]=='D'){
                if(br.empty()) continue;
                br.pop();
                --brz;
            }
            if(str[0]=='L'){
                if(!br.empty()){
                    int temp=br.top();
                    br.pop();
                    --brz;
                    af.push(temp);
                    ++afz;
                }
            }
            if(str[0]=='R'){
                if(!af.empty()){
                    int temp=af.top();
                    af.pop();
                    br.push(temp);
                    --afz;
                    sum[++brz]=sum[brz-1]+temp;
                    Max[brz]=max(Max[brz-1],sum[brz]);
                }
            }
            if(str[0]=='Q'){
                scanf("%d",&x);
                if(x>=brz)
                    printf("%d
    ",Max[brz]);
                else
                    printf("%d
    ",Max[x]);
            }
        }
        }
        return 0;
    }
  • 相关阅读:
    DAY 06 PYTHON入门
    DAY 05 PYTHON入门
    DAY 04 PYTHON入门
    DAY 03 PYTHON入门
    DAY 02 PYTHON入门
    DAY 01 PYTHON入门
    RTC时钟和BKP的配置stm32
    Altium Designer画原理图时要紧凑
    串口的数据长度校验
    Centos7下创建文件夹 移动文件/文件夹 删除文件/文件夹 解压/文件夹打包压缩 命令整理
  • 原文地址:https://www.cnblogs.com/wangfang20/p/3277622.html
Copyright © 2011-2022 走看看