kedaOJ#P2849时间涟漪
题目
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;
}
}
}
}
这是mcr130102的博客,转载请注明出处