1. Problem Detail - 【入门】火车编组 - 追梦算法网
栈的练习题
#include#include #include #include #include using namespace std; const int N = 100; int main() { stack nums; int code[100]; int n,num,t = 1; cin>>n; for(int i = 1; i <= n;i++){ cin>>code[i]; nums.push(i); cout<<"A"; while(!nums.empty()&&nums.top() == code[t]&&t <= i){ cout<<"B"; nums.pop(); t++; } } while(!nums.empty()){ nums.pop(); cout<<"B"; } return 0; }
2.Problem Detail - 离间计 - 追梦算法网
虽然在栈的分类里面,但最后还是用了数组
#includeusing namespace std; const int N=1e5+7; int w[N]; int main(){ int t,n; cin>>t; while(t--){ cin>>n; for(int i=0;i =0;j--){ if(w[j] 3.Problem Detail - QAQ和车库 - 追梦算法网
#includeusing namespace std; const int N=1e6+7; int SS[N],top=-1; int n,k,v,maxx=0; int main() { scanf("%d",&n); while(n--){ scanf("%d",&k); if(k==1){ scanf("%d",&v); if(v>maxx)SS[++top]=v,maxx=v; else SS[++top]=maxx; }else if(k==2){ maxx=SS[--top]; }else{ if(top==-1) printf("%dn",-1); else printf("%dn",SS[top]); } } return 0; } 4.Problem Detail - A Famous City - 追梦算法网
#include#include using namespace std; stack build; int main() { ios::sync_with_stdio(false); int n,k,x; cin>>n; for(int j = 1; j <= n; j++){ int ans = 0l; cin>>k; for(int i = 0; i < k; i++){ //通过让建筑保持单调递增的情况,从而使每清除一个建筑就代表数量加一 cin>>x; while(!build.empty()&&build.top()>x){ ans++; build.pop(); } if(x == 0)continue; if(build.empty()||build.top() < x){ build.push(x); } } while(!build.empty()){ ans++; build.pop(); } cout<<"Case "<



