UVa514
模拟题做的不多,在这一方面比较弱。直接按书上的打了,再慢慢理解,消化。一开始还不理解的,后来看着书上的图就懂了。(因为输入不同,此代码并不能在oj上ac)有一组数据很奇怪,试了几次有不同的结果。
#include<iostream>
#include<stack>
using namespace std;
#include<stack>
using namespace std;
int const maxn=1000+10;
int target[maxn];
int main()
{
int T,x;
while(cin>>T)
{
stack<int> s;
int A=1,B=1;
for(int i=1;i<=T;i++)
{
cin>>target[i];
}
int ok=1;
while(B<=T)
{
if(A==target[B]) {A++;B++;} //进栈后马上出栈
else if(!s.empty()&&s.top()==target[B]) {s.pop();B++;} //出栈
else if(A<=T) s.push(A++); //进栈
else {ok=0;break;}
}
if(ok) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
int target[maxn];
int main()
{
int T,x;
while(cin>>T)
{
stack<int> s;
int A=1,B=1;
for(int i=1;i<=T;i++)
{
cin>>target[i];
}
int ok=1;
while(B<=T)
{
if(A==target[B]) {A++;B++;} //进栈后马上出栈
else if(!s.empty()&&s.top()==target[B]) {s.pop();B++;} //出栈
else if(A<=T) s.push(A++); //进栈
else {ok=0;break;}
}
if(ok) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
顺便把另外一题代码贴上 UVa12100
#include<bits/stdc++.h>
using namespace std;
using namespace std;
int main()
{
int T,n,x,target,m,step;
cin>>T;
while(T--)
{
queue<int> q;
priority_queue<int> pq;
step=0;
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>x;
q.push(x);
pq.push(x);
if(i==m) target=x;
}
while(!pq.empty()&&pq.top()>=target)
{
while(q.front()!=pq.top())//移动不需要时间
{
q.push(q.front());
q.pop();
m=m==0?q.size()-1:m-1;
}
q.pop();
pq.pop();
++step;--m;
if(m<0) break; //目标打印完成
}
cout<<step<<endl;
}
return 0;
}