救救救救救救救救救救孩子吧!!!!!
查看原帖
救救救救救救救救救救孩子吧!!!!!
1036002
yjjh楼主2024/3/7 12:58

用栈写的

找了半天BUG都没找到

救救孩子吧!!!

代码如下:

#include <bits/stdc++.h>
using namespace std;
stack<int>gbq;
stack<int>gbh;
int f[1114514],s[1114514],tt;
int maa(){
    s[tt]=s[tt-1]+gbq.top();
    f[tt]=max(f[tt-1],s[tt]);
}
int ii(int x){
    gbq.push(x);
    tt++;
    maa(); 
}
int dd(){
    gbq.pop();
    if(tt>0){
        tt--;
        maa();
    }	
}
int ll(){
    if(gbq.size()!=0){
        int o=gbq.top();
        gbq.pop();
        gbh.push(o);
    }
}
int rr(){
    if(gbh.size()!=0){
        int o=gbh.top();
        gbh.pop();
        gbq.push(o);
        tt++;
        maa();
    }
}
int qq(int k){
    cout <<f[k]<<endl;
}
int main(){
    int n;
    cin >>n;
    for(int i=0;i<n;i++){
        char c;
        int x,k;
        cin >>c;
        if(c=='I'){
            cin >>x;
            ii(x);
        }else if(c=='D')dd();
        else if(c=='L')ll();
        else if(c=='R')rr();
        else if(c=='Q'){
            cin >>k;
            qq(k);
        }
    }
    return 0;
}

求求啦!!!

2024/3/7 12:58
加载中...