30分的题果然没那么简单,自己写超时三个。。。但还是记录一下啊:
大神的归纳:https://blog.csdn.net/sinat_29278271/article/details/47291659
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> st;
vector<int> temp;
int main(){
int N,key,i,j;
char com[11];
string str;
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%s",com);
str=com;
if(str=="Pop"){
if(st.size()==0)
printf("Invalid\n");
else{
printf("%d\n",st[st.size()-1]);
st.pop_back();
}
}
else if(str=="Push"){
scanf("%d",&key);
st.push_back(key);
//printf("%d\n",key);
}
else if(str=="PeekMedian"){
if(st.size()==0)
printf("Invalid\n");
else{
temp.clear();
for(j=0;j<st.size();j++)
temp.push_back(st[j]);
sort(temp.begin(),temp.end());
if(temp.size()%2)
key=(temp.size()+1)/2-1;
else key=temp.size()/2-1;
printf("%d\n",temp[key]);
//st.pop_back();
}
}
}
system("pause");
return 0;
}