出题原意是考双端队列,但是因为A题要让大家都过,所以把数据搞得很小。
第一份代码是纯数组模拟栈暴搞的,遇到大数据会完蛋,第二份是用双端队列模拟,可以胜任大数据的情况。
/*
纯数组模拟栈暴搞
*/
#include <iostream>
using namespace std;
int S[10005];
int K, top , m, x, bottom;
int main()
{
string s;
cin >> K >> m;
while(m--) {
cin >> s;
if(s == "push") {
cin >> x;
S[top++] = x;
if(top - bottom > K) bottom++;
} else {
if(top == bottom) cout << "Empty!" << endl;
else cout << S[--top] << endl;
}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int max_size, m;
deque<int> dq;
int main() {
scanf("%d %d", &max_size, &m);
while(m--) {
char op[16]; scanf("%s", op);
if(op[1] == 'u') {
int x; scanf("%d", &x);
dq.push_back(x);
if(dq.size() > max_size) dq.pop_front();
} else if(op[1] == 'o') {
if(dq.size() == 0) puts("Empty!");
else /* */ printf("%d\n", dq.back()), dq.pop_back();
}
}
return 0;
}