2929 - Menor da Pilha
Usando pilhas de um jeito um pouquinho diferente...
Last updated
Usando pilhas de um jeito um pouquinho diferente...
Last updated
#include <stdio.h>
int minimos[1000000];
int main(){
char operacao[4];
int N, V, minimo, p;
p = -1;
scanf("%d\n", &N);
for(int i = 0; i < N; ++i){
scanf("%s", &operacao);
if(!memcmp(operacao, "PUSH", 4)){
scanf("%d\n", &V);
if(p == -1){
minimo = V;
}else{
minimo = (minimos[p] < V ? minimos[p] : V);
}
minimos[++p] = minimo;
}else if(!memcmp(operacao, "POP", 3)){
if(p == -1){
printf("EMPTY\n");
}else{
--p;
}
}else{
if(p == -1){
printf("EMPTY\n");
}else{
printf("%d\n", minimos[p]);
}
}
}
return 0;
}#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int main(){
int N, V;
string operacao;
stack<int> minimos;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
for(int i = 0; i < N; ++i){
cin >> operacao;
if(operacao == "PUSH"){
cin >> V;
if(minimos.empty()){
minimos.push(V);
}else{
minimos.push(min(minimos.top(), V));
}
}else if(operacao == "POP"){
if(minimos.empty()){
cout << "EMPTY" << endl;
}else{
minimos.pop();
}
}else{
if(minimos.empty()){
cout << "EMPTY" << endl;
}else{
cout << minimos.top() << endl;
}
}
}
return 0;
}