题目
kedaOJ#P2849时间涟漪
思路
栈
代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
stack<int> timeRipples;
int maxEnergy = INT_MIN;
for (int i = 0; i < N; ++i) {
int instruction;
cin >> instruction;
if (instruction == 1) {
int energy;
cin >> energy;
timeRipples.push(energy);
if (energy > maxEnergy) {
maxEnergy = energy;
}
} else if (instruction == 2) {
if (!timeRipples.empty()) {
int topEnergy = timeRipples.top();
timeRipples.pop();
if (topEnergy == maxEnergy) {
// 重新计算最大能量
maxEnergy = INT_MIN;
stack<int> tempStack;
while (!timeRipples.empty()) {
int energy = timeRipples.top();
timeRipples.pop();
tempStack.push(energy);
if (energy > maxEnergy) {
maxEnergy = energy;
}
}
while (!tempStack.empty()) {
timeRipples.push(tempStack.top());
tempStack.pop();
}
}
}
} else if (instruction == 3) {
if (!timeRipples.empty()) {
cout << maxEnergy << endl;
} else {
cout << 0 << endl;
}
}
}
}
标签:kedaOJ,tempStack,int,energy,maxEnergy,timeRipples,instruction,涟漪,P2849
From: https://www.cnblogs.com/mcr130102/p/18262323