A. 文件传输

    Type: Default 1000ms 256MiB

文件传输

No testdata at current.

You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.


#include <bits/stdc++.h>
#include <stack>
using namespace std;

int main() {
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int N;
    cin >> N;  // 读取操作总数
    
    stack<int> stk;          // 主栈:存储货物重量(后进先出)
    stack<int> max_stk;      // 辅助栈:存储当前仓库的最大重量(与主栈同步)
    
    while (N--) {            // 遍历每个操作
        int op;
        cin >> op;
        
        if (op == 0) {       // 操作0:入库(格式:0 X)
            int X;
            cin >> X;
            stk.push(X);     // 重量入主栈
            
            // 辅助栈维护当前最大值:空栈则存X,否则存“栈顶与X的较大者”
            if (max_stk.empty()) {
                max_stk.push(X);
            } else {
                max_stk.push(max(max_stk.top(), X));
            }
        } else if (op == 1) { // 操作1:出库(格式:1)
            // 栈非空时才处理,避免空栈操作
            if (!stk.empty()) {
                stk.pop();   // 弹出最晚入库的货物
                max_stk.pop(); // 辅助栈同步弹出,保持状态一致
            }
        } else if (op == 2) { // 操作2:查询(格式:2)
            if (max_stk.empty()) { // 仓库为空时输出0
                cout << "0\n";
            } else { // 否则输出当前最大重量(辅助栈顶)
                cout << max_stk.top() << "\n";
            }
        }
    }
    
    return 0;
}

寒假集训

Not Attended
Status
Done
Rule
IOI
Problem
2
Start at
2026-2-3 14:00
End at
2026-2-4 14:00
Duration
24 hour(s)
Host
Partic.
63