L2-4 彩虹瓶 (25分)
输入样例:
7 5 3
7 6 1 3 2 5 4
3 1 5 4 2 6 7
7 6 5 4 3 2 1
输出样例:
YES
NO
NO
注意: 判断是否超出货架容量时,不能直接跳出来,要把数据读完,不然第一个测试点会错。标记一下就行。
具体AC代码如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,k;
cin>>n>>m>>k;
while(k--){
int t=1,flag=0,x;
stack<int> st;
for(int i=1; i<=n; i++){
cin>>x;
if(x==t){
t++;
while(!st.empty()&&st.top()==t){
st.pop();
t++;
}
}else{
st.push(x);
if(st.size()>m) flag=1; //不能没读完数据跳出来,不然第一个数据点过不了
}
}
if(!st.empty()||flag) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
return 0;
}
欢迎大家批评改正!!!